These are two different events related to APEX Trigger. When you save a record (insert / update) Salesforce fires a DML statement to its database, internally.
All the code written in the "before update" triggers, executes BEFORE that DML is committed to Salesforce Database. Code written in after trigger executes AFTER the commit is made.
Hence if you are trying to update any value in the record, on which the trigger is fired, you need not write an update call, specifically. But you need to do it in after triggers.
example, if a before trigger is written on Opportunity and you want to change a value of one of the fields it would be
for(Opportunity o: Trigger.new)
{
o.dummyfield__c = 'some value';
}
This will make the change to Opportunity record, as Salesforce will itself make a commit to database. No update call is required.
Whereas in After trigger you have to use the update call on Opportunity, to make this update happen as Salesforce has already Committed to the database.
I am trying update an existing Account record.But i am getting this error"System.NullPointerException: Attempt to de-reference a null object: Trigger.BeforeInsertAndUpdate: line 36, column 21". I don't know what is the reason. Can you tell me where i was stuck.
Here is my code:
trigger BeforeInsertAndUpdate on Account (before insert, before update) {
Hi,
You can find the info on this Apex documentation.
All Answers
Hi,
You can find the info on this Apex documentation.
These are two different events related to APEX Trigger. When you save a record (insert / update) Salesforce fires a DML statement to its database, internally.
All the code written in the "before update" triggers, executes BEFORE that DML is committed to Salesforce Database. Code written in after trigger executes AFTER the commit is made.
Hence if you are trying to update any value in the record, on which the trigger is fired, you need not write an update call, specifically. But you need to do it in after triggers.
example, if a before trigger is written on Opportunity and you want to change a value of one of the fields it would be
for(Opportunity o: Trigger.new)
{
o.dummyfield__c = 'some value';
}
This will make the change to Opportunity record, as Salesforce will itself make a commit to database. No update call is required.
Whereas in After trigger you have to use the update call on Opportunity, to make this update happen as Salesforce has already Committed to the database.
Thank you very much...
Hi Imran,
I am trying update an existing Account record.But i am getting this error"System.NullPointerException: Attempt to de-reference a null object: Trigger.BeforeInsertAndUpdate: line 36, column 21". I don't know what is the reason. Can you tell me where i was stuck.
Here is my code:
trigger BeforeInsertAndUpdate on Account (before insert, before update) {
Set<String> peset=new Set<String>();
Map<String,Account> emaileadMap=new Map<String,Account>();
if (Trigger.isInsert) {
for(Account acc:Trigger.new){
if(acc.PersonEmail!=null){
peset.add(acc.PersonEmail);
}
}
List<Account> ll;
if(peset!=null && peset.size()>0)
{
ll = [select id,personemail,phone from Account where personemail in : peset];
}
if(ll!=null && ll.size()>0){
for(Account lrec:ll){
emaileadMap.put(lrec.personemail,lrec);
}
}
List<Account> aclist;
for(Account acc:Trigger.new){
if(acc.PersonEmail!=null){
if(emaileadMap.containsKey(acc.PersonEmail)){
Account l=emaileadMap.get(acc.PersonEmail);
l.phone=acc.phone;
aclist.add(l);/*----------------Line No:36---------------------*/
}
}
}
update aclist;
}
}
Thanks in Advance.
initialize List<Account> aclist; to
List<Account> aclist = new List<Account>();