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
Haider NaseemHaider Naseem 

Bulk Apex Trigger Error

Hi,

I get the below error when executing the trigger for the challenge:

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, DynastyRoot: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.DynastyRoot: line 8, column 1: []

The challenge is:To complete this challenge, you need to add a trigger for Opportunity. The trigger will add a task to any opportunity inserted or updated with the stage of 'Closed Won'. The task's subject must be 'Follow Up Test Task'.
The Apex trigger must be called 'ClosedOpportunityTrigger'
With 'ClosedOpportunityTrigger' active, if an opportunity is inserted or updated with a stage of 'Closed Won', it will have a task created with the subject 'Follow Up Test Task'.
To associate the task with the opportunity, fill the 'WhatId' field with the opportunity ID.
This challenge specifically tests 200 records in one operation.


Code:

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

    List<Task> taskList = new List<Task>();
    
    for(Opportunity opp : Trigger.new) {
        
        //Only create Follow Up Task only once when Opp StageName is to 'Closed Won' on Create
        if(Trigger.isInsert) {
            if(Opp.StageName == 'Closed Won') {
                taskList.add(new Task(Subject = 'Follow Up Test Task', WhatId = opp.Id));
            }
        }
        
        //Only create Follow Up Task only once when Opp StageName changed to 'Closed Won' on Update
        if(Trigger.isUpdate) {
            if(Opp.StageName == 'Closed Won' 
            && Opp.StageName != Trigger.oldMap.get(opp.Id).StageName) {
                taskList.add(new Task(Subject = 'Follow Up Test Task', WhatId = opp.Id));
            }
        }       
    }

    if(taskList.size()>0) {        
        insert taskList;        
    }    
}

 
Best Answer chosen by Haider Naseem
Nagendra ChinchinadaNagendra Chinchinada
Hi Haider,

It seems like there is trigger on Tasks 'DynastyRoot' which is active and causing null pointer exception in it. Go to Task triggers and deactivate DynastyRoot trigger(if it is not required) and check the challenge. It should work.
Let me know if it works.

Thanks,
Nagendra
 

All Answers

GauravGargGauravGarg

Hi Haider,

Can you post the code for Trigger.DynastyRoot trigger.

Thanks,

Gaurav

Nagendra ChinchinadaNagendra Chinchinada
Hi Haider,

It seems like there is trigger on Tasks 'DynastyRoot' which is active and causing null pointer exception in it. Go to Task triggers and deactivate DynastyRoot trigger(if it is not required) and check the challenge. It should work.
Let me know if it works.

Thanks,
Nagendra
 
This was selected as the best answer
Haider NaseemHaider Naseem
Thank you so much Nagendra! I was unable to understand what the error was pointing to. Lesson learnt :)
Thanks Again!