You need to sign in to do that
Don't have an account?

APEX Trigger Button and Assignment Problem
Below is my code which works on a custom lead object. The work I am doing is on a force.com org which does not have the standard lead object. On the custom lead object I have a check box called convert. When checked a trigger fires and using the data contained in the lead it creates a contact and an account.
Problem 1 - I want to link the account and contact but I can't get it to work, I guess it's because the account is not yet created
Problem 2 - Instead of checking the tick box I would instead like a detail page button when clicked it would fire the trigger below
trigger ConvertTrigger on fp_lead__c (after insert, after update) { System.Debug('## >>> Trigger executing run by ' + UserInfo.getName()); fp_lead__c[] myLeadsToProcess = new fp_lead__c[]{}; for(Integer i=0 ; i<Trigger.new.size() ; i++) { if ( Trigger.new[i].convert__c == true ) { myLeadsToProcess.add(Trigger.new[i]); // build list of recs to process } } if(myLeadsToProcess.size()>0){ Contact[] myNewContact = new Contact[]{}; Account[] myNewAccount = new Account[]{}; for (fp_lead__c myLead : myLeadsToProcess) { Account act = new Account(); act.name = myLead.company__c; act.website = myLead.website__c; myNewAccount.add(act); Contact con = new Contact(); con.firstname = myLead.First_Name__c; con.lastname = myLead.Last_Name__c; con.phone = myLead.Phone__c; con.mobilephone = myLead.Mobile__c; con.fax = myLead.Fax__c; con.email = myLead.Email__c; con.title = myLead.Title__c; con.salutation = myLead.Salutation__c; myNewContact.add(con); } insert myNewContact; insert myNewAccount; } System.Debug('## >>> Trigger END <<<'); }
You could either :
1) Expose a web method which can be invoked on a custom button click to execute the conversion code
OR
2) Use a Custom VF Page with a Custom Controller, which is invoked on the button click.
My personal preference is Option 2, coz it might be easier to display any errors to the User.
Here's a good post on how to invoke Apex Code use a custom VF page on a button click :
http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/
As for the linking problem, you need to insert the Account first and then associate the Account Id with the Contact, before inserting it.
Given that this solution will not be a bulkified solution, as it only needs to be invoked on the single record on a button click, it could probably read
insert Account;
contact.AccountId = Account.Id;
insert contact;
All Answers
You could either :
1) Expose a web method which can be invoked on a custom button click to execute the conversion code
OR
2) Use a Custom VF Page with a Custom Controller, which is invoked on the button click.
My personal preference is Option 2, coz it might be easier to display any errors to the User.
Here's a good post on how to invoke Apex Code use a custom VF page on a button click :
http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/
As for the linking problem, you need to insert the Account first and then associate the Account Id with the Contact, before inserting it.
Given that this solution will not be a bulkified solution, as it only needs to be invoked on the single record on a button click, it could probably read
insert Account;
contact.AccountId = Account.Id;
insert contact;
Thanks, on problem 2 I simply move the insert account above the contact build.
Cheers
R