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
ColaCola 

Apex Trigger Not Updating with Proper Date

We created two triggers (one for leads and one for contacts) that populate a lead/contact field with the date of a task that is associated with that lead/contact. The trigger works just fine with contacts, however with leads we have the following issue:

Task is created with an ActivityDate of 5/20/2014 and the trigger sets the date on the lead level as TODAY (ex. 5/30/2014).

This is the code (bold part is where the error is occuring):

trigger UpdateInTouchLead on Task (after insert, after update) {
Set<String> whoIds = new Set<String>();

    for (Task t : Trigger.new) {
    whoIds.add(t.WhoId);
}

    List<Lead> leads = [SELECT Id, First_in_touch__c FROM Lead WHERE Id =: whoIds];
   
    Map<String, Task> taskMap = new Map<String, Task>();

    for (Task t : Trigger.new){
     if (!(t.Type.equals('Eloqua Activity'))) {
      if (t.Status.equals('Completed')) {
       taskMap.put(t.WhoId, t);
      }
     }
}
        
    for (Lead l : leads) {
    if (taskMap.containsKey(l.Id)) {
      l.First_in_touch__c = taskMap.get(l.Id).ActivityDate;
   l.Status = 'In Touch';
   l.POC_Communications__c = 'Muted';
    }
}
update leads;

}


Any idea on why this isn't working as desired?
Best Answer chosen by Cola
ColaCola
Found the issue Ramu_SFDC, there was an active workflow rule that was applying a datestamp to that field. (Which explains why only the leads were being affected)

All Answers

Ramu_SFDCRamu_SFDC
Can you add a system.debug statement for Activitydate just before the bold line in your code and tell me the output if it is showing some other date or today's date. Honestly, I have not seen any issues with the code. If it is just a day difference I would have suspected the timezone factor but in the current scenario there is a difference of 10 days which is really strange.
ColaCola

I inserted a system.debug statement just before the bolded line, and I check the values of both the activity date and the first in touch date and I received the following results: 

|DEBUG|Activity Date: 2014-05-20 00:00:00
|DEBUG|First In Touch null

which is what I would expect. 

And then after the bolded line, i put another debug to see the value of first in touch and it reads: 

|DEBUG|First In Touch 2014-05-20 00:00:00 

Which is again what i would expect. But then the test case still fails. Let me know if there is something I am missing. 

ColaCola
Found the issue Ramu_SFDC, there was an active workflow rule that was applying a datestamp to that field. (Which explains why only the leads were being affected)
This was selected as the best answer