+ Start a Discussion
NBP1411NBP1411 

Apex Trigger for Alternate Contact Conversion on a Lead

Hi, I have 4 fields in leads (Alternate Contact First Name, Last Name, Alt. Phone, and Alt. Email) and I am hoping to write an Apex Trigger where when the lead is converted, an extra Contact is created based on the Alternate Contact's info. How can I do this?  (That is, create the main contact through the conversion and then an extra contact if the fields are filled out.)
Shashikant SharmaShashikant Sharma
Hi,

You need to write a trigger on Lead Convert. Read the values from Lead and insert new instance of Contact.

Your trigger code will be like below. Change API Names as per your field api names.
trigger test on Lead (after update) {
 
List<Contact> altCons = new List<Contact>();
for(Lead lead:System.Trigger.new) {
  if (Lead.IsConverted)
  //change API Names as per yours
  Contact altCon = new Contact( FirstName = Alternate_First_Name__c, 
                                                           LastName = Alternate_First_Name__c,  
                                                           Phone = Alt_Phone__c,
                                                           Email = Alt_Email__c );

  altCons.add( altCon ); 
}

if( altCons.size() > 0 ) {
insert altCons;
}

}

Thanks
Shashikant
NBP1411NBP1411
Hi Shashikant,

Thank you for this response. This is really helpful! For some reason it is not working when I insert it like this:
trigger test on Lead (after update) {
 
List<Contact> altCons = new List<Contact>();
for(Lead lead:System.Trigger.new) {
  if (Lead.IsConverted)
  //  Contact altCon = new Contact(FirstName = Alt_First_Name__c,
                                                           LastName = Alt_Last_Name__c, 
                                                           Phone = Alt_Phone__c,
                                                           Email = Alt_Email__c );

  altCons.add( altCon ); 
}

if( altCons.size() > 0 ) {
insert altCons;
}

}


Any suggestions? Should I include "Create" as a command with "Contact altCon"?


Thanks,
Tom