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
KRayKRay 

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
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';
		}
	}
}

 
Best Answer chosen by KRay
Maharajan CMaharajan C
Hi XRay,

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

Nayana KNayana K
trigger TaskInsertTrigger on Task (after insert) {

change it to
trigger TaskInsertTrigger on Task (before insert) {

Maharajan CMaharajan C
Hi XRay,

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
This was selected as the best answer
KRayKRay
Hey Maharaja C, 

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. 
Maharajan CMaharajan C
Can you please mark the best answer which one helps to fix the issue.So it will help others!!!

Thanks,
Raj