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
Yash JagtapYash Jagtap 

Create task on leads with selected status using trigger.

trigger TaskOnLeadStatusTrigger on Lead (after insert, after update) {
     
    List<Task> taskList = new List<Task>();
    
    // Add a task for each Lead.
    // Iterate over Leads those are inserted recently.
          for (Lead ld : Trigger.New) 
        {
        // Add a task for this lead with selected status and subject = 'Lead status changed'.
        taskList.add(new Task(WhoId = ld.OwnerId, Subject='Lead status changed"'+ld.Status+'". Start the next process')); 
    }
    
    if (taskList.size() > 0) 
        insert taskList;
        
}
Why is it not working, my goal is to create a task on leads with selected status using trigger. with WhoId i am trying to link Ids.
Best Answer chosen by Yash Jagtap
Khushmeet KaurKhushmeet Kaur
Hi Yash,
As the WhoId can only refer to the Lead or Contact id, it can't be referred to the user record.
If you want to assign the lead owner the task please use task OwnerId