0

I have a use case to link a case records created for a lead record. If lead record gets converted then the related cases should be automatically linked to the converted contact and account record. Please help to check and correct my code below (I am new here, so your help is appreciated.)

public class LeadTriggerHandler {
    public static void updateRelatedContactWithCase(List<Lead> leList, Map<Id, Lead> leOldMap){
        
        List<case> caList = new List<case>();
        Set<Id> leIds = new Set<Id>();
        
        for(Lead le : leList){
            if(le.Status == 'Closed - Converted' && leOldMap.get(le.Id).Status != 'Closed - Converted'){
                if(le.Status == 'Closed - Converted')
                    leIds.add(le.Id);
            }
            System.debug(leIds);
        }
        // To update Lead related Contact to link the case record related to the Lead which was converted.
        if(!leIds.isEmpty()){
            for(Lead le = [SELECT Id, Status,
                           (SELECT Id, Lead__c, AccountId, ContactId FROM Cases__r) FROM Lead WHERE Id IN: leIds]){
                               if(le.cases__r !=null){
                                   for(Cases__r ca : le.Cases__r){
                                       ca.ContactId = le.ConvertedContactId;
                                       caList.add(ca);
                                       
                                   }
                               }
                               if(!caList.isEmpty()){
                                   update caList;
                               }
                               
                           }
        }
        
    }
}

1 Answer 1

0

You need to check the isConverted attribute of the lead also to gain more limit on your process i suggest using FUTURE method. Note, The Cases will be visible after some delay not immediately, If you want instant view after conversion do not use FUTURE

public class LeadTriggerHandler {
     public static void updateRelatedContactWithCase(List<Lead> leList, Map<Id, Lead> leOldMap){
        Set<Id> convertedLeadIds = new Set<Id>();
        for(Lead currentLead : leList){
            //conversion is Happening
            if(currentLead.IsConverted){
                convertedLeadIds.add(currentLead.Id)
            }
        }
        if(convertedLeadIds.size() > 0 ){
            //Logic here. 
            //Make a future call to update cases  
            LeadTriggerHandler.processRecords(convertedLeadIds);

        }
     }
    @future
    public static void processRecords(Set<ID> recordIds)
    {   
         // Your logic here 
    }
} 
2
  • Thank you Avijit for your help and time!
    – user136633
    Commented Jul 20, 2023 at 18:24
  • Hello Avijit, here is the code I wrote and it's working as per my expectations. But could you please check if it's correct. Thank you
    – user136633
    Commented Aug 1, 2023 at 8:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .