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

Test Task Trigger

I have a trigger on Task (after insert, after update) that I'm attempting to create test code for.  Below is a partial snapshot of the test class.  


Lead l1001b = new Lead(Company='Test1001b', LastName='Test1001b', OwnerID=UserId, NumberOfEmployees=1001);
Lead[] ld = new Lead[] {l1001b};
insert ld;


Where I'm having trouble is, I need to create a Task that is associated to the Lead record however, I receive the error "Field is not writeable: Who" with the syntax I have below. 


Task t1001b = new Task(Who=l1001b, Owner=UserId, Subject='Call', Status='Completed', Priority='Normal');
Task[] t = new Task[] {t1001b};
insert t;


I need to be able to create Tasks assocated to Leads and Contacts in order to adequately test my trigger.  Can anyone point me in the right direction?



Does WhoID work?

WhoId works. I had tried that earlier but recieved an error. This morning I must be more awake as I realized the t1001b was not the Lead Id but instead the Lead Owner Id. Once I corrected that oversight WhoId worked just fine. Thanks for giving me reason to look deeper.