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
PatcsPatcs 

what is the difference between "before update" and "after update"?

what is the difference between "before update" and "after update"?

Best Answer chosen by Admin (Salesforce Developers) 
Imran MohammedImran Mohammed

Hi,

 

You can find the info on this Apex documentation.

 

 

Trigger Event  Can change fields usingtrigger.new
 Can update original object using an update DML operation 
Can delete original object using a delete DML operation
beforeupdateAllowed.Not allowed. A runtime error is thrown.Not allowed. A runtime error is thrown.
afterupdateNot allowed. A runtime error is thrown, astrigger.new is already saved.Allowed. Even though a bad script could cause an infinite recursion doing this incorrectly, the error would be found by the governor limits.Allowed. The updates are saved before the object is deleted, so if the object is undeleted, the updatesbecome visible.

All Answers

Imran MohammedImran Mohammed

Hi,

 

You can find the info on this Apex documentation.

 

 

Trigger Event  Can change fields usingtrigger.new
 Can update original object using an update DML operation 
Can delete original object using a delete DML operation
beforeupdateAllowed.Not allowed. A runtime error is thrown.Not allowed. A runtime error is thrown.
afterupdateNot allowed. A runtime error is thrown, astrigger.new is already saved.Allowed. Even though a bad script could cause an infinite recursion doing this incorrectly, the error would be found by the governor limits.Allowed. The updates are saved before the object is deleted, so if the object is undeleted, the updatesbecome visible.
This was selected as the best answer
MiddhaMiddha

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.

 

PatcsPatcs

Thank you very much...

Dev-FoxDev-Fox

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.

MiddhaMiddha

initialize  List<Account> aclist; to 

 

List<Account> aclist = new List<Account>();