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
Salesforce Dev in TrainingSalesforce Dev in Training 

Custom Object Mapping when Lead converts to a Contact

Hello SF Dev Community,

I hope everyone had a great weekend! I am trying to map a custom object from a Lead to a Contact when that Lead is converted. I see that it's an "Idea" posted and most of the comments say it's an easy fix with Apex coding, however, as a non-profit organization, it's tough sometimes to afford high-end development. I wasn't certain if this easy fix is something I could do as I've recently started to learn Apex coding. Any thoughts or instructions would greatly help.
 

Thank you,
 

AB

Best Answer chosen by Salesforce Dev in Training
Pankaj_GanwaniPankaj_Ganwani
Hi Andrews,

Thanks for confirming. Please paste below mentioned trigger code for accomplishing this functionality. The trigger will be on lead:
 
trigger LeadTrigger on Lead (after update) 
{
    if(Trigger.isAfter && Trigger.isUpdate)
    {
        List<Survey__c> lstSurvey = new List<Survey__c>();
        Set<Id> convertedLeads = new Set<Id>();
        for(Lead objLead : Trigger.new)
        {
            if(objLead.isConverted)
                convertedLeads.add(objLead.Id);
        }
        
        for(Survey__c objSurvey : [select Lead__c from Survey__c where Lead__c IN : convertedLeads])
        {
            objSurvey.Lead__c = null;
            lstSurvey.add(objSurvey);
        }
        
        update lstSurvey;
    }
}

 

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi Andrew,

As per my understanding by going through your above statements, there is a custom object which is related to Lead. On conversion of lead, you want some fields of this custom object should be mapped with created Contact. Please correct me if I am wrong. If so, this is doable by Apex.
Salesforce Dev in TrainingSalesforce Dev in Training
I actually want the whole custom object to transfer over to the Contact. This object exists on both the Lead and Contact, however, when I convert a Lead to a Contact, it disappears.
Pankaj_GanwaniPankaj_Ganwani
So, this custom object is working as joiner object between Lead and Contact via lookup relationship?
Salesforce Dev in TrainingSalesforce Dev in Training
Yes, that's correct. Both Lead and Contact have a "Lookup" relationship to "Surveys" (the custom object).
Pankaj_GanwaniPankaj_Ganwani
Hi Andrew,

Thanks for the clarifications. Can you please confirm one last thing that you want to put blank value in Lead lookup field on related Survey object record and want to keep the value of Contact intact post lead conversion?

If so, this can be easily done using trigger approach as workflow and process builder approach are getting failed here.
Salesforce Dev in TrainingSalesforce Dev in Training
If I'm understanding this correctly... I believe yes we would want the Contact lookup to be populated as opposed to the Lead (since the Lead was converted).
Pankaj_GanwaniPankaj_Ganwani
Hi Andrews,

Thanks for confirming. Please paste below mentioned trigger code for accomplishing this functionality. The trigger will be on lead:
 
trigger LeadTrigger on Lead (after update) 
{
    if(Trigger.isAfter && Trigger.isUpdate)
    {
        List<Survey__c> lstSurvey = new List<Survey__c>();
        Set<Id> convertedLeads = new Set<Id>();
        for(Lead objLead : Trigger.new)
        {
            if(objLead.isConverted)
                convertedLeads.add(objLead.Id);
        }
        
        for(Survey__c objSurvey : [select Lead__c from Survey__c where Lead__c IN : convertedLeads])
        {
            objSurvey.Lead__c = null;
            lstSurvey.add(objSurvey);
        }
        
        update lstSurvey;
    }
}

 
This was selected as the best answer
sumit singh 37sumit singh 37
trigger LeadTrigger on Lead (after update) 
{
    if(Trigger.isAfter && Trigger.isUpdate)
    {
        List<Survey__c> lstSurvey = new List<Survey__c>();
        Set<Id> convertedLeads = new Set<Id>();
        id convertcontact ;
        for(Lead objLead : Trigger.new)
        {
            if(objLead.isConverted)
           {
                convertedLeads.add(objLead.Id);
                convertcontact = ConvertedContactId ;
           }
        }
        
        for(Survey__c objSurvey : [select Lead__c,Contact__c from Survey__c where Lead__c IN : convertedLeads])
        {
            objSurvey.Lead__c = null;
           objSurvey.contact__c=convertcontact ; 

            lstSurvey.add(objSurvey);
        }
        
        update lstSurvey;
    }
}

 
sumit singh 37sumit singh 37
this will transfer the Survey to Converted Contact.
Salesforce Dev in TrainingSalesforce Dev in Training
Thank you so much. I will try it out.