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
Dosbol TDosbol T 

Please help with post deployment code.

Hello,
I am trying to update all my leads which were created before I have added a new field. Now, after having all deployed onto UAT I need to perform post deployment action. But having an issue with the code, what is wrong with it?
For (List <Event> eventList: [SELECT Id, WhoId, TypeOf     
 WHO 
 WHEN Lead 
 THEN FirstInterviewdate__c 
 END, Subject, ActivityDate 
 FROM Event 
 WHERE Subject = 'First Interview'
AND
 WHOID != null
 LIMIT 200]){
    List<Lead> leadlist = new List<Lead>(); 
    For(Event evt: eventList){
        leadlist.add(new Lead (Id=evt.WhoId, FirstInterviewdate__c = evt.ActivityDate));
      }
    update leadList;
}
and the issue is:

User-added image
 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Disbol,

Can you explain more about your post deployment step?

what exactly you need to update in lead records?

Thanks!!
Dosbol TDosbol T
Hi Ankaiah,
Sure. The field FirstInterviewdate__c is the new field which was added later. But we had already leads created before that field was added, now this is field is empty for those old leads. I need to update them with this code above.
AnkaiahAnkaiah (Salesforce Developers) 
As per my understanding, you need to update the old leads with  ActivityDate field of event object. Am i correct?
Dosbol TDosbol T
Yes, correct.
AnkaiahAnkaiah (Salesforce Developers) 
Just one query, one lead have multiple events then how can you identify which event record data you need to update on Lead.? is there any condition to fetch the event records?
 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Dosbol,

Execute the below code in developer console.
 
Map<Id,Date> whoidwithDate = new Map<Id,Date>();

List<EVENT> eventlist = [Select id,ActivityDate, whoid from Event where subject='First Interview'];
for(EVENT ev: eventlist){
whoidwithDate.put(ev.whoid,ev.ActivityDate);
}
System.debug('whoidwithDate=='+whoidwithDate);
System.debug('whoidwithDate.values()=='+whoidwithDate.values());
//Select id,first_Interview_Date__c,

List<Lead> leadlist = [Select id,first_Interview_Date__c from lead where first_Interview_Date__c=Null AND id=:whoidwithDate.keyset()];
system.debug('leadlist'+leadlist);
for(Lead ld:leadlist){
    
    If(whoidwithDate.containskey(ld.id)){
        
        ld.first_Interview_Date__c=whoidwithDate.get(ld.id);
        
    }
}
Update leadlist;
If this helps, Please mark it as best answer.

Thanks!!
 
Dosbol TDosbol T
Hi Ankaiah,
Thanks for your effort. Unfortunately, it did not work. I have slightly updated my own code, but it is not helping either. What is wrong?
For (List <Event> eventList: [SELECT Id, WhoId, TypeOf WHO 
                              WHEN Lead 
                              THEN FirstInterviewdate__c 
                              END, Subject, ActivityDate 
                              FROM Event 
                              WHERE Subject = 'First Interview'
							  AND
							  WHOID != null
                              LIMIT 200]){
    List<Lead> leadlist = new List<Lead>(); 
    For(Event evt: eventList){
        String whoid2 = evt.WHOID;
        IF(whoid2.startswith ('00Q')){
			leadlist.add(new Lead (Id=evt.WhoId, 
								FirstInterviewdate__c = evt.ActivityDate));
       }
    }
    update leadList;
}

 
AnkaiahAnkaiah (Salesforce Developers) 
I have tested in my demo org and my code working fine.

when you executed my code, is there any error you are getting?
Dosbol TDosbol T
No error in your code, the only thing is its not updating the leads on my UAT.
AnkaiahAnkaiah (Salesforce Developers) 
can you check the debug logs for my code.. Its working for me 
AnkaiahAnkaiah (Salesforce Developers) 
I have tested your code also, its working fine.