I have this code that creates a task when opportunity is closed won, however it needs to be assigned to one specific person only.

trigger ClosedOpportunityTrigger on Opportunity (after insert, after update)
    List<Task> tasks = new List<Task>(); 
    for (Opportunity Opp : Trigger.new) 
        if(Opp.StageName == 'Closed Won')
        Task tsk = new Task(whatID = Opp.ID, subject='Follow Up Test Task'); 
    insert tasks; 

What am I missing? Thanks

1 Answer 1


You are missing an assignment to the OwnerId field of the Tasks. Since hard-coding Ids is a bad practice, you may want to store the desired ownership in a Custom Setting that your trigger can access efficiently so that you can maintain this solution in the future.

Note also that your trigger will create a task any time an Opportunity is modified if its stage is Closed Won - not exactly once when the Opportunity enters that stage. If the latter behavior is what you want, you'd need to do something like

if (opp.StageName == 'Closed Won' && Trigger.oldMap.get(opp.Id).StageName != 'Closed Won') {

(That's applicable only to update, of course, as insert has no oldMap).

  • Thanks David, I will adjust the code to add that. Yeah I dont want to hard code the Id. I know thats bad practice. And yes its the latter behavior. Only when the opportunity enters closedwon. Nov 3, 2020 at 20:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .