Skip to main content
Hi,

I'm getting stuck on the bulk apex trigger challenge on trailhead and keep recieving the same error:

Challenge Not yet complete... here's what's wrong: 

There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ClosedOpportunityTrigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.ClosedOpportunityTrigger: line 9, column 1: []

I've tried creating new playgrounds for just htis challenge after seeing advice on here but I still get the same error.

Here is the trigger that I wrote, I cant find any reason for this error to happen.

trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {

    List<Task> taskList;

    for(Opportunity opp : Trigger.new){

        if(opp.StageName == 'Closed Won'){

            taskList.add(new Task(whoId=opp.id,subject='Follow Up Test Task'));

        }

    }

    insert taskList;

}

And this is the trailhead im attempting: https://trailhead.salesforce.com/trails/force_com_dev_beginner/modules/apex_triggers/units/apex_triggers_bulk
5 answers
  1. Jan 23, 2020, 4:18 PM
    I made changes to the above code to have it execute:

     

    trigger ClosedOpportunityTrigger on Opportunity (before insert, before update) {

    List<Task> tasks = New List<Task>();

    for (opportunity opp:trigger.new) {

    if (opp.stagename=='Closed Won') {

    task t=new Task(

    whatid=opp.id,

    Status = 'Active',

    Subject = 'Follow Up Test Task',

    ActivityDate = system.today()

    );

    tasks.add(t);

    }

    }

    insert tasks;

    }

     
0/9000