You need to sign in to do that
Don't have an account?
Help: Can't change Task's WhoId value.
Hi everybody. We have a trigger on task object which works before insert and changes WhoId value like this:
trigger x on Task (before insert ) { Set<String> taskIds = new Set<String> (); for(Task t : trigger.new) taskIds.add(t.WhatId); Map<id, Custom_Object__c> mas = new Map<id, Custom_Object__c>([Select Id, Account__r.PersonContactId From Custom_Object__c where Account__r.IsPersonAccount = true and Id in : taskIds]); for(Task t : trigger.new) { Custom_Object__c a = mas.get(t.WhatId); if(a == null) continue; t.WhoId = a.Account__r.PersonContactId; } }
When I create a task record. The created tasks whoId field is always NULL. When I check the debug logs, I can clearly see this trigger works and assings a new whoid value. But somehow in the end, it always returns to null and there are no errors, or warnings...
When I run someting like update new Task(ID='XXX', WhoID='YYY'); (where the values of XXX is a Task ID and YYY is a PersonAccount Contact ID) from the developer console, I can see that WhoID value is changing.
We are using Person Accounts. The Custom_Object__c has a lookup field for Account object named Account__c.
What do you think, what is the problem of this code?
Please update the question with proper __c and __r .The code in some places refres __r and some place __c
Hello mohit_shrivastava. I've updated the original post with the proper tags. Sorry for the inconvenience.
Can you add a debug statement in the last loop
for(Task t : trigger.new) {
Custom_Object__c a = mas.get(t.WhatId);
system.debug('=========== custom object: ' + a);
if(a == null) continue;
t.WhoId = a.Account__r.PersonContactId;
}
Let me know the output.
erakan,
I seem to be having the same problem, did you ever find a solution?
TIA,
Chad
I've created a scheduleable batch class to do the same job as trigger, and scheduled it to work every minute. I can't understand why but the trigger just doesn't work.
Thanks for the suggestion. I've opened a case with Salesforce Support, I'll report back on if we can come up with a solution.
It's a bug. If you have the ability to schedule an Activity against multiple contacts you cannot update the WhoID in a regular fashion. What you can do, is your batch idea, tech support also suggest a future method. So my standard trigger calls a future method that actually does all the processing.
Chad, Can you please elaborate on this "So my standard trigger calls a future method that actually does all the processing." ? How is this done? Please point me to some sample code.