You need to sign in to do that
Don't have an account?

BeforeInsert Trigger error
Hello,
I have created a trigger that updates a hidden field on an Account record that was needed in order to track changes on a seperate field that is a formula field. When the field value changes, the trigger runs and captures the previous value in the Account History. Below is my trigger code (in Sandbox) that works perfect:
trigger BusinessRatingChange on Account (before insert, before update)
{
for(Account a:Trigger.new)
{
if(system.trigger.OldMap.get(a.Id).Business_Rating__c != system.trigger.NewMap.get(a.Id).Business_Rating__c)
{
a.Business_Rating_History__c = a.Business_Rating__c;
}
}
}
However, when I attempt to create a NEW account, I get the below error that looks like is due to the "BeforeInsert" action on the trigger:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger BusinessRatingChange caused an unexpected exception, contact your administrator: BusinessRatingChange: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.BusinessRatingChange: line 5, column 1
Does anyone have any insight as to how I can remedy this? Thanks so much in advance for your assistance!!!
Eric
I have created a trigger that updates a hidden field on an Account record that was needed in order to track changes on a seperate field that is a formula field. When the field value changes, the trigger runs and captures the previous value in the Account History. Below is my trigger code (in Sandbox) that works perfect:
trigger BusinessRatingChange on Account (before insert, before update)
{
for(Account a:Trigger.new)
{
if(system.trigger.OldMap.get(a.Id).Business_Rating__c != system.trigger.NewMap.get(a.Id).Business_Rating__c)
{
a.Business_Rating_History__c = a.Business_Rating__c;
}
}
}
However, when I attempt to create a NEW account, I get the below error that looks like is due to the "BeforeInsert" action on the trigger:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger BusinessRatingChange caused an unexpected exception, contact your administrator: BusinessRatingChange: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.BusinessRatingChange: line 5, column 1
Does anyone have any insight as to how I can remedy this? Thanks so much in advance for your assistance!!!
Eric
If you are planning to add more logic that requires you to make it a before insert trigger as well, then this should work:
All Answers
for code specific to update logic.
I would also revisit the trigger logic. If you are inserting a new record, you really don't need to check the 'old' value of a field -- all values are new.
If you are planning to add more logic that requires you to make it a before insert trigger as well, then this should work:
Also, below is another way of providing update checking in your trigger. Behaves the same as sbb's excellent example, just more food for thought:
Thank you both again...so very helpful!!!