function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
thekid12345thekid12345 

How to create a trigger to automatically convert a lead which creates an account, contact, and opportunity?

How do I go about doing this?
Andrew EchevarriaAndrew Echevarria
Hey there, do you have experience coding in Apex? You would write a trigger than copies information from your lead. See this article on how to write a trigger: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_trigger.htm 

Here's an example of a partial solution of a trigger to create a contact and copy the name. You can follow the methodology to make other objects too and copy other fields:
trigger sampleTrigger on Lead (after update) {

   for(Lead l : trigger.new){
       Contact c = new Contact(Name = l.Name);
        insert c;
  }  
}


 
thekid12345thekid12345
I have a little bit of experience with APEX but still learning :).  So for Accounts and Opportunities I would have 
for(Lead l : trigger.new){
       Contact c = new Contact(Name = l.Name);
        insert c;
       Account a = new Account(Name = l.Name);
       insert a;
       Opportunity o = new Opportunity(Name = l.Name);
       insert o;
}

or is there more to this?
Andrew EchevarriaAndrew Echevarria
Very nice! It depends on if there are more fields you'd like to copy from the lead, then you can add them in the brakets, example: (Name = l.Name, field = l.field).

In addition, under what condition do you want this to occur? Surely not just whenever any lead is updated. You should wrap those statements in an if statement! If you don't know how let me know and I can help you write it.
thekid12345thekid12345
Thanks Andrew! I will update my answer soon and can you review it for me?
Andrew EchevarriaAndrew Echevarria
For sure :)