You need to sign in to do that
Don't have an account?

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?
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?
All Answers
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.