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
HeeseHeese 

Help with Trigger

Hi all,

 

I'm looking for some guidance on how to modify this trigger.  I downloaded the App Exchange App "Custom Object Lead Conversion" by Hi-Soft and I need to slightly edit the code to fit my use case.  What I've noticed is the Trigger does not bring over multiple records of the Custom Object when it's converted.  It seems to bring only the 1st record while disregarding the rest.  I am not a programmer so any help would be great.  Below is the unmodified code from the Trigger.  Thanks in advance

 

 

trigger UpdateCustomeObject_Trigger on Lead (before update) {
//This trigger will associate a Custom Object record with the contact and opportunity associated to the 
//lead after it has been converted.
//The Custom Object is associated to an opportunity only if an opportunity record exist on the Lead.
    for (Integer i = 0; i < Trigger.new.size(); i++){
        if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false){
            Set<Id> leadIds = new Set<Id>();
            for (Lead lead : Trigger.new) 
                leadIds.add(lead.Id);
            
            Map<Id, CustomObject__c> entries = new Map<Id, CustomObject__c>([select Contact__c, Opportunity__c, Account__c, Lead__c from CustomObject__c where lead__c in :leadIds]);        
            if(!Trigger.new.isEmpty()) {
                for (Lead lead : Trigger.new)  {
                    for (CustomObject__c CustomObject : entries.values()) {
                        if (CustomObject.Lead__c == lead.Id) {
                            CustomObject.contact__c = lead.ConvertedContactId;
                            CustomObject.opportunity__c = lead.ConvertedOpportunityId;
                            CustomObject.account__c = lead.ConvertedAccountId;
                            update CustomObject;
                            break;
                        }
                    }
                }
            }
        }
    }

}

 

 

hitzhitz

Hi, Heese

 

Try This If it full fill your requirments.......................

 

 

trigger UpdateCustomeObject_Trigger on Lead (before update) {
    //This trigger will associate a Custom Object record with the contact and opportunity associated to the
    //lead after it has been converted.
    //The Custom Object is associated to an opportunity only if an opportunity record exist on the Lead.
    
    for (Lead ld : Trigger.New){
        if (ld.IsConverted == true && ld.isConverted == false){
            Set<Id> leadIds = new Set<Id>();
            for (Lead lead : Trigger.new)
                leadIds.add(lead.Id);
                
            Map<Id, CustomObject__c> entries = new Map<Id, CustomObject__c>([
                        select Contact__c,
                        Opportunity__c,
                        Account__c,
                        Lead__c
                        from CustomObject__c
                        where lead__c in :leadIds]);
            
            if(!Trigger.new.isEmpty()) {
                for (Lead lead : Trigger.New) {
                    for (CustomObject__c CustomObject : entries.values()) {
                        if (CustomObject.Lead__c == lead.Id) {
                            CustomObject.contact__c = lead.ConvertedContactId; C
                            ustomObject.opportunity__c = lead.ConvertedOpportunityId;
                            CustomObject.account__c = lead.ConvertedAccountId;
                            
                            update CustomObject; break;
                        }
                    }
                }
            }
        }
    }
}

HeeseHeese

Hi Hitz,

 

Thanks for your reply!  I just tested your code and it seems to drop both records now from the Custom Object upon conversion.  

HeeseHeese

Is there something else that may be causing the drop?  Thanks

BrandiTBrandiT

Were you able to get this trigger to work?  I'm having the exact same problem.

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!!