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
advlgxadvlgx 

Updating converted lead ids with the new contact id in a child custom object...

Is it possible to initiate/write something where the child records of a custom object that has both a contactid and leadid fields in the record, get updated within or after the Salesforce Convert Lead process executes, with the new contactid that was created from the converted lead?
shillyershillyer

Yes, using either Apex Code or the API, you can programmatically call the ConvertLead operation and then update your custom object contactID field with the values from the ConvertLead result.

 

Hope that helps,

Sati

advlgxadvlgx

Not wanting to actually remove the existing ConvertLead process, is this something I would do with a trigger? In other words, what do you recommend as the process for invoking the custom code while maintaining the basic Convert Lead functionality in Salesforce?

 

Thanks

DevNVDevNV

Hi there. 

 

You can definitely do this with a trigger and continue to use the OOTB Lead conversion process.  If you have an object with a lookup field to Leads (say LeadChild) then also add a lookup to the Contact object.  Create a trigger on the Lead object (after update) and check to see if the Lead was just converted.  If it was, cycle through any LeadChild records linked to that Lead and update the Contact lookup field with the Lead's new Converted Contact Id.  This should move the LeadChild records from Lead over to Contact during the Conversion.  You could do this same thing if your custom object needs to transfer to an Account or Opportunity, although you can't be guaranteed that an Opportunity is created each time a Lead is converted.

 

Hope that helps,

Niki

Vankerk Solutions

BrandiTBrandiT

Did you ever figure this out?  I'm having the same issue.  I've tried a couple of different unsuccessful trigger codes, but cannot make it work.

 

Could you share the code you used for this?

 

Thanks a lot!!!

BrandiTBrandiT

I found a wonderful resource that was able to help me with this.  Here is the code I'm using:

 

trigger UpdateOHObject_Trigger on Lead (after update) {

  Map<Id, Lead> leadMap = new Map<Id,Lead>();
  Lead parent;
 
  for (Integer i = 0; i < Trigger.new.size(); i++){
    if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false) {
      leadMap.put( Trigger.new[i].Id, Trigger.new[i]);
    }
  }
   
  if( leadMap.size() > 0 ) {
      Set<Id> leadIds = leadMap.keySet();
      List<Opportunity_Headings__c> allChildren =
        [select Id, Opportunity__c, Account__c, Lead__c from Opportunity_Headings__c where lead__c in :leadIds];      
 
 System.debug(allChildren);
   
      for ( Opportunity_Headings__c child : allChildren ) {
        if ( leadMap.containsKey( child.Lead__c ) ) {
           // lookup the parent lead
           parent = leadMap.get( child.Lead__c );
           // update the fields on the child object
           child.opportunity__c = parent.ConvertedOpportunityId;
           child.account__c = parent.ConvertedAccountId;
        }
      }

System.debug(allChildren);

    //try {
      update allChildren;
   // } catch( Exception e ) {
         // could put something here to notify on error
         // otherwise it fails silently
   // }
     
  }
}

 

 

Thanks again Robert!!