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
Russell baker 1Russell baker 1 

Trigger to create contacts through lead conversion

Hi,
I have created some custom fields on lead object to capture alternate contact details apart from main contact.
First_Other_Contact_Name__c
First_Other_Contact_Phone__c
First_Other_Contact_Email__c
Second_Other_Contact_Name__c
Second_Other_Contact_Phone__c
Second_Other_Contact_Email__c

My requirement is when we normally convert lead so it become Account, opportunity and contact. I want those custom contacts details should become as second and third contact details under the account.
How can I achieve this. Please help!
Please find below code which is not working as expected:
trigger LeadConvert on Lead (after update) {

    map<Id, Lead> mapNewLead = trigger.newMap;
    List<Contact> lstContact = new List<Contact>();
    for(Lead objLead: mapNewLead.values())
    {
       if (objLead.isConverted == true) 
       {
         lstContact.add(new contact(LastName = objLead.First_Other_Contact_Name__c));
         lstContact.add(new contact(Phone = objLead.First_Other_Contact_Phone__c));
         lstContact.add(new contact(Email = objLead.First_Other_Contact_Email__c));
       }
    }
    insert lstContact;
}

 
Best Answer chosen by Russell baker 1
Russell baker 1Russell baker 1
trigger LeadConvert on Lead (after update) { 

    map<Id, Lead> mapNewLead = trigger.newMap; 
    List<Contact> lstContact = new List<Contact>(); 
    for(Lead objLead: mapNewLead.values()) 
    { 
        if (objLead.isConverted == true ) 
        { 
            lstContact.add(new contact(LastName = objLead.First_Other_Contact_Name__c,Phone = objLead.First_Other_Contact_Phone__c, 
            Email = objLead.First_Other_Contact_Email__c,AccountId = objLead.convertedAccountId));
            lstContact.add(new contact(LastName = objLead.Second_Other_Contact_Name__c,Phone = objLead.Second_Other_Contact_Phone__c, 
            Email = objLead.Second_Other_Contact_Email__c,AccountId = objLead.convertedAccountId));
            lstContact.add(new contact(LastName = objLead.Third_Other_Contact_Name__c,Phone = objLead.Third_Other_Contact_Phone__c, 
            Email = objLead.Third_Other_Contact_Email__c,AccountId = objLead.convertedAccountId));
            
        } 
    insert lstContact; 
}
}

This code is working absolulty fine for me.

All Answers

bob_buzzardbob_buzzard
'Not working as expected' doesn't give us much to go on. Are you seeing errors, does the trigger fire?

If you want these contacts to be associated with the account, you'll need to set the accountId field, otherwise they will be private contacts to the user that carried out the conversion. You can find the account id in the 'ConvertedAccountId' field in the lead record.
Russell baker 1Russell baker 1
No error, when I convert lead it is converted but no contact has been created through the custom fields.
Prafull G.Prafull G.
when you added Contact to list..everytime you are creating a new instance. Additionally, as @bob_buzzard mentioned, you need to link this contact to account as well. Modify code within if statement to have one instance and then try. It should work.
Russell baker 1Russell baker 1
trigger LeadConvert on Lead (after update) { 

    map<Id, Lead> mapNewLead = trigger.newMap; 
    List<Contact> lstContact = new List<Contact>(); 
    for(Lead objLead: mapNewLead.values()) 
    { 
        if (objLead.isConverted == true ) 
        { 
            lstContact.add(new contact(LastName = objLead.First_Other_Contact_Name__c,Phone = objLead.First_Other_Contact_Phone__c, 
            Email = objLead.First_Other_Contact_Email__c,AccountId = objLead.convertedAccountId));
            lstContact.add(new contact(LastName = objLead.Second_Other_Contact_Name__c,Phone = objLead.Second_Other_Contact_Phone__c, 
            Email = objLead.Second_Other_Contact_Email__c,AccountId = objLead.convertedAccountId));
            lstContact.add(new contact(LastName = objLead.Third_Other_Contact_Name__c,Phone = objLead.Third_Other_Contact_Phone__c, 
            Email = objLead.Third_Other_Contact_Email__c,AccountId = objLead.convertedAccountId));
            
        } 
    insert lstContact; 
}
}

This code is working absolulty fine for me.
This was selected as the best answer
Samuel Hoffman 2Samuel Hoffman 2
Make sure you have "Require Validation for Converted Leads" enabled under Lead Settings.  If you don't see the option to enable it, contact Salesforce Customer Support to have them open up that option for you.