+ Start a Discussion
MaheemSamMaheemSam 

Bulkfy the code

Hi,

  I have a helperclass code which is working perfect in salesforce issue is when data is loaded most of the records are not getting update Please let me know what might be the issue with code below Please suggest.
public class CampaignMemberTriggerUtils{

   public static void processResponseDateUpdate(List<CampaignMember> newRec){
       List<Lead> leadLst = new List<Lead>();
       List<Contact> contLst = new List<Contact>();
       Set<Id> leadIdLst = new Set<ID>();
       Set<Id> ContIdLst = new Set<ID>();
       Map<ID, datetime> lastCamResDateMap = new Map<ID, datetime>();
       for(CampaignMember cm : newRec) {
           if(cm.LeadId != null)
             leadIdLst.add(cm.LeadId);
           if(cm.ContactId != null)
             contIdLst.add(cm.ContactId);
       }
       
       List<CampaignMember> camMems = [SELECT Id, LeadId, ContactId, CampaignId, Status, FCRM__FCR_Response_Date__c FROM CampaignMember 
                   where (LeadId = :leadidLst or ContactId = :contIdLst) and FCRM__FCR_Response_Date__c <> null order by FCRM__FCR_Response_Date__c desc];
       for(CampaignMember cam : camMems) {
           if(!lastCamResDateMap.containsKey(cam.LeadId))
               lastCamResDateMap.put(cam.LeadId, cam.FCRM__FCR_Response_Date__c);
           if(!lastCamResDateMap.containsKey(cam.ContactId))
               lastCamResDateMap.put(cam.ContactId, cam.FCRM__FCR_Response_Date__c);
       }
           
       for( ID leadid : leadIdLst) {
        leadLst.add(new Lead(id = leadid, Last_Campaign_Response_Date__c = lastCamResDateMap.get(leadid)));
       }
        for( ID contactid : contIdLst) {
        contLst.add(new Contact(id = contactid,Last_Campaign_Response_Date__c =  lastCamResDateMap.get(ContactId)));
       }
       system.debug(leadLst + '_'+  contLst);
       if(!leadLst.isEmpty())
         update leadLst; 
       if(!contLst.isEmpty())
         update contLst; 
   } 
    
}

Thanks
Sudhir
NForceNForce
Hi Sudhir,
Looks like code is already bulkified, what's the error you got?

Thanks,
NForce