You need to sign in to do that
Don't have an account?
KRay
Update WhatId on Task insert
Hey Forum,
I'm working with email-to-case and task creation when emails are received. Creating tasks when emails are received is simple, however, populating the "whatId" field is becoming a pain in my butt. I want to populate the Task WhatId field using the contactID that's associated to the case. This sounds pretty straightforward but the field isn't updating on insert. Even, if I hardcode the Id, the field never updates. Can you tell me what I'm doing wrong?
TRIGGER
HELPER CLASS
NOTE: This is NOT "bulkfied". I've stripped away ALL of the logic for testing purposes!
I'm working with email-to-case and task creation when emails are received. Creating tasks when emails are received is simple, however, populating the "whatId" field is becoming a pain in my butt. I want to populate the Task WhatId field using the contactID that's associated to the case. This sounds pretty straightforward but the field isn't updating on insert. Even, if I hardcode the Id, the field never updates. Can you tell me what I'm doing wrong?
TRIGGER
trigger TaskInsertTrigger on Task (after insert) { TaskTriggerHelper helper = new TaskTriggerHelper(); if(trigger.isBefore && trigger.isInsert){ helper.handleAfterInsert(Trigger.new); } }
HELPER CLASS
NOTE: This is NOT "bulkfied". I've stripped away ALL of the logic for testing purposes!
public class TaskTriggerHelper{ public TaskTriggerHelper() { } public void handleAfterInsert(List<Task> taskList){ List<Task> tasksToUpdate = new List<Task>(); for( Task taskRecord : taskList ){ taskRecord.WhatId = [SELECT ID, ContactId FROM CASE WHERE id = :taskRecord.WhatId].ContactId; taskRecord.Subject = 'This is a Test'; } } }
You can't use the WhatId to lookup the Contact from case in Task Object because its a standard lookup of Account,Opportunity,Case,Solution...
So you have to use the WhoId to Lookup the Contact from case in Task Object becaues its a standard lookup field of Contact,Lead.
And also please use my below trigger to you testing purpose,
trigger autoCreateTask on Case (after insert)
{
List<Task> insertTask = new List<Task>();
for(Case newCase : Trigger.new)
{
Task newTask = new Task();
newTask.subject = 'New Case Task';
newTask.whatId = newCase.Id;
newTask.whoId=newCase.ContactId;
newTask.ownerId = newCase.OwnerId;
newTask.status = 'In progress';
newTask.Priority = 'Normal';
insertTask.add(newTask);
}
if(insertTask.size() > 0)
insert insertTask;
}
Can you please try the above things and let me know if it works or not?
Mark this as solved if it's resolved.
Thanks,
Raj
All Answers
change it to
You can't use the WhatId to lookup the Contact from case in Task Object because its a standard lookup of Account,Opportunity,Case,Solution...
So you have to use the WhoId to Lookup the Contact from case in Task Object becaues its a standard lookup field of Contact,Lead.
And also please use my below trigger to you testing purpose,
trigger autoCreateTask on Case (after insert)
{
List<Task> insertTask = new List<Task>();
for(Case newCase : Trigger.new)
{
Task newTask = new Task();
newTask.subject = 'New Case Task';
newTask.whatId = newCase.Id;
newTask.whoId=newCase.ContactId;
newTask.ownerId = newCase.OwnerId;
newTask.status = 'In progress';
newTask.Priority = 'Normal';
insertTask.add(newTask);
}
if(insertTask.size() > 0)
insert insertTask;
}
Can you please try the above things and let me know if it works or not?
Mark this as solved if it's resolved.
Thanks,
Raj
I totally had a NOOB moment, I was trying to populate the "WHATID" field instead of the "WHOID" field with the associated contact. After flipping the fields, it worked fine! Thank you for being a second set of eyes.
Thanks,
Raj