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
Vidhyasagar RanganathanVidhyasagar Ranganathan 

trigger to update custom object upon lead conversion

Hi All,

I am looking for a trigger on Lead that upon lead conversion would update a custom object (CAT) with two fields. The custom object already has Lead ID's. The trigger would need to find the lead ID's in the custom object and update values of two fields from lead object.

Any help is appreciated greatly !

Thanks
Best Answer chosen by Vidhyasagar Ranganathan
Anuj_KrAnuj_Kr
Hi Vidhyasagar

Please have a look on below code and let me know if this will full fill you requirement or not.
 
trigger updatedCustomObject on Lead (After Update) {

    Map<id,Lead> leadWIthIdMap = new Map<id,Lead>([select id,name,title from lead where IsConverted = true  
        and Id in : trigger.newMap.keySet()]);
    List<CAT__c> customObjList = new List<CAT__c>();
    for(CAT__c obj : [select id, name, Lead_Title__c, Lead__c from CAT__c where Lead__c != null and Lead__c In : leadWithIdMap.keySet()]){
        
        Lead le = leadWithIdMap.get(obj.Lead__c);
        
        obj.Name = le.name;
        obj.Lead_Title__c = le.title;
        customObjList.add(obj);
    }
    
    if(customObjList.size() > 0)
        Database.update(customObjList);
}

 

All Answers

Anuj_KrAnuj_Kr
Hi Vidhyasagar

Please have a look on below code and let me know if this will full fill you requirement or not.
 
trigger updatedCustomObject on Lead (After Update) {

    Map<id,Lead> leadWIthIdMap = new Map<id,Lead>([select id,name,title from lead where IsConverted = true  
        and Id in : trigger.newMap.keySet()]);
    List<CAT__c> customObjList = new List<CAT__c>();
    for(CAT__c obj : [select id, name, Lead_Title__c, Lead__c from CAT__c where Lead__c != null and Lead__c In : leadWithIdMap.keySet()]){
        
        Lead le = leadWithIdMap.get(obj.Lead__c);
        
        obj.Name = le.name;
        obj.Lead_Title__c = le.title;
        customObjList.add(obj);
    }
    
    if(customObjList.size() > 0)
        Database.update(customObjList);
}

 
This was selected as the best answer
Rahul.MishraRahul.Mishra
Hi Vidhyasagar,

I have written the sample code fo you, you have to do slight modification (Your Object name and fields name which are you updating) to work it for you.
 
trigger test on Lead (before update) {

Map<Id, Lead> mapOfIdandLead = new Map<Id, Lead>();
List<CAT__c> lstCAT = new List<CAT__c>();


 for(Lead lead:System.Trigger.new) {
  if (Lead.IsConverted)
   mapOfIdandLead.put(lead.Id, lead);
 }
 
 
 //I am assuming your custom object name is CAT__c and field name id lead__c. You are trying to update field1__c and Field2__c.
 
    for (CAT__c CT : [Select Id, lead__c, field1__c, Field2__c From CAT__c Where lead__c =:mapOfIdandLead.keySet()) {
	
	   lstCAT.add(new CAT__c(Id = CT.Id, field1__c = Lead.FirstName, Field2__c = Lead.LastName));
	}
	
	
	if(!lstCAT.isEmpty())
	  update lstCAT;
}

Mark solved if it does help you.