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
RiothamusRiothamus 

Null object in insert trigger

I'm modifying a trigger that I wrote to fire during an insert, as well as an update. When processing an update, the object returend from Trigger.new is NULL, and I get a NULL pointer exception. How can I get the object being inserted?

 

I tried it using after insert, and before insert. They both fail:

 

// set district when Account is inserted or updated

trigger AccountDistrictTrigger on Account (after insert, before update) {

    // run trigger on update object(s)
   AccountDistrictUpdater updater = new AccountDistrictUpdater();
   for (Account account: Trigger.new) {
     updater.executeTrigger(account);  // FAILS, account IS NULL
   }
}

 

// set district when Account is inserted or updated
trigger AccountDistrictTrigger on Account (before insert, before update) {

    // run trigger on update object(s)
   AccountDistrictUpdater updater = new AccountDistrictUpdater();
   for (Account account: Trigger.new) {
     updater.executeTrigger(account);  // FAILS, account IS NULL

   }
}

 

How do I get the object?

 

Best Answer chosen by Admin (Salesforce Developers) 
logontokartiklogontokartik

There I see the issue

 

On Before or After Insert Triggers you will not have OldMap. I am not sure what you are doing with Old Object, but if you want to set it, just do it on Update trigger, you can do it as 

// set district when Account is inserted or updated
trigger AccountDistrictTrigger on Account (after insert, before update) {
    // run trigger on update object(s)
   AccountDistrictUpdater updater = new AccountDistrictUpdater();
   for (Account account: Trigger.new) {
     updater.executeTrigger(account, Trigger.isUpdate); // will pass true if update and false if insert 
   }
}

 Change your method to 

 

public void executeTrigger(SObject obj, boolean isUpdate) {
  if(isUpdate){
  SOBject old_obj = Trigger.oldMap.get(obj.ID); // this is line 28
 }

 Hope you are able to fix it.

 

All Answers

logontokartiklogontokartik

Your trigger code looks good with after insert. You should not be getting a Null from Trigger.New, It might be your Class AccountDistrictUpdater which is failing. Can you paste the debug log ?

RiothamusRiothamus

Hi,

Here is the debug log:

26.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
17:03:42.252 (252502000)|EXECUTION_STARTED
17:03:42.252 (252542000)|CODE_UNIT_STARTED|[EXTERNAL]|01qZ00000004MRz|AccountDistrictTrigger on Account trigger event AfterInsert for [001Z000000G3z8T]
17:03:42.270 (270786000)|METHOD_ENTRY|[10]|01pZ00000004zT4|AccountDistrictUpdater.AccountDistrictUpdater()
17:03:42.270 (270929000)|METHOD_EXIT|[10]|AccountDistrictUpdater
17:03:42.270 (270968000)|CONSTRUCTOR_ENTRY|[5]|01pZ00000004zT4|<init>()
17:03:42.271 (271046000)|METHOD_ENTRY|[10]|01pZ00000004zSV|BaseDistrictUpdater.BaseDistrictUpdater()
17:03:42.271 (271063000)|METHOD_EXIT|[10]|BaseDistrictUpdater
17:03:42.271 (271073000)|CONSTRUCTOR_ENTRY|[19]|01pZ00000004zSV|<init>()
17:03:42.271 (271158000)|CONSTRUCTOR_EXIT|[19]|01pZ00000004zSV|<init>()
17:03:42.271 (271167000)|CONSTRUCTOR_EXIT|[5]|01pZ00000004zT4|<init>()
17:03:42.271 (271394000)|SYSTEM_METHOD_ENTRY|[6]|LIST<Account>.iterator()
17:03:42.271 (271689000)|SYSTEM_METHOD_EXIT|[6]|LIST<Account>.iterator()
17:03:42.271 (271721000)|SYSTEM_METHOD_ENTRY|[6]|system.ListIterator.hasNext()
17:03:42.271 (271762000)|SYSTEM_METHOD_EXIT|[6]|system.ListIterator.hasNext()
17:03:42.271 (271863000)|METHOD_ENTRY|[7]|01pZ00000004zSV|BaseDistrictUpdater.executeTrigger(SObject)
17:03:42.272 (272013000)|METHOD_EXIT|[7]|01pZ00000004zSV|BaseDistrictUpdater.executeTrigger(SObject)
17:03:42.272 (272143000)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Class.BaseDistrictUpdater.executeTrigger: line 28, column 1
Trigger.AccountDistrictTrigger: line 7, column 1
17:03:42.272 (272166000)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Class.BaseDistrictUpdater.executeTrigger: line 28, column 1
Trigger.AccountDistrictTrigger: line 7, column 1
17:03:42.330 (272224000)|CUMULATIVE_LIMIT_USAGE
17:03:42.330|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of script statements: 6 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

17:03:42.330|CUMULATIVE_LIMIT_USAGE_END

17:03:42.272 (272285000)|CODE_UNIT_FINISHED|AccountDistrictTrigger on Account trigger event AfterInsert for [001Z000000G3z8T]
17:03:42.272 (272296000)|EXECUTION_FINISHED

Here is executeTrigger:

public void executeTrigger(SObject obj) {

  SOBject old_obj = Trigger.oldMap.get(obj.ID); // this is line 28

 

I tried this, and still get a NULL pointer error: 


SOBject old_obj = obj.ID == null ? null : Trigger.oldMap.get(obj.ID);

 

Another approach would be to pass isNew as a flag to the executeTrigger, and modify behavior based on that

logontokartiklogontokartik

There I see the issue

 

On Before or After Insert Triggers you will not have OldMap. I am not sure what you are doing with Old Object, but if you want to set it, just do it on Update trigger, you can do it as 

// set district when Account is inserted or updated
trigger AccountDistrictTrigger on Account (after insert, before update) {
    // run trigger on update object(s)
   AccountDistrictUpdater updater = new AccountDistrictUpdater();
   for (Account account: Trigger.new) {
     updater.executeTrigger(account, Trigger.isUpdate); // will pass true if update and false if insert 
   }
}

 Change your method to 

 

public void executeTrigger(SObject obj, boolean isUpdate) {
  if(isUpdate){
  SOBject old_obj = Trigger.oldMap.get(obj.ID); // this is line 28
 }

 Hope you are able to fix it.

 

This was selected as the best answer
RiothamusRiothamus

Thanks, works great !!