You need to sign in to do that
Don't have an account?
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?
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
Change your method to
Hope you are able to fix it.
All Answers
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 ?
Hi,
Here is the debug log:
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
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
Change your method to
Hope you are able to fix it.
Thanks, works great !!