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
Jeff BomarJeff Bomar 

Should my apex trigger be before update or after update

I was wondering if someone could help me answer a question. Should my apex trigger be before update or after update. unfortunately, I have to write an apex trigger for this and can't use process builder because I have to grab the unit price from the price book and perform a formula to charge the customer correctly. If I want to perform the update only when the field Total hours and Type are updated. and I don't want the trigger to change anything when any other field is updated. would that trigger be a Before or After. thanks for your help.
Best Answer chosen by Jeff Bomar
ANUTEJANUTEJ (Salesforce Developers) 
Hi Jeff,

Generally, we use after trigger when we want the Id of the record we are creating so that it can be used as a value for look up in child record, so for example when the sample scenario is we create a contact for every account created we would be using after trigger so that the accountid which is a lookup field in contact needs to be the newly created recordid and create the necessary contact records as per the necessary details.

The before trigger is used when we want to change the values of the field or fetch a certain value from a related object and assign the value to the field before inserting there by making sure the value is correct, for example if we want to make sure for every contact record the checkbox field is true only if there are certain tasks under the account then we fetch the necessary values evaluate and make checkbox value as per the scenario.

For more details you can try checking this link: https://www.sfdc99.com/2014/01/25/use-vs-triggers/

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Jeff,

Generally, we use after trigger when we want the Id of the record we are creating so that it can be used as a value for look up in child record, so for example when the sample scenario is we create a contact for every account created we would be using after trigger so that the accountid which is a lookup field in contact needs to be the newly created recordid and create the necessary contact records as per the necessary details.

The before trigger is used when we want to change the values of the field or fetch a certain value from a related object and assign the value to the field before inserting there by making sure the value is correct, for example if we want to make sure for every contact record the checkbox field is true only if there are certain tasks under the account then we fetch the necessary values evaluate and make checkbox value as per the scenario.

For more details you can try checking this link: https://www.sfdc99.com/2014/01/25/use-vs-triggers/

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
This was selected as the best answer
Jeff BomarJeff Bomar
Thank you for your quick answer that helps a lot will start doing some research how to create this trigger.
ANUTEJANUTEJ (Salesforce Developers) 
You are welcome, Jeff. I appreciate it if you can close the query by marking it as solved. It may help others in the community. Thank You!