After apex trigger
After triggers are triggers that run after a record is saved to the database. These triggers are used to automate business processes once the record is successfully saved to the database.
Record Id's are generated during the save process, and after triggers can be used to create or update related records in the database using these record ids.
An example of an 'after' trigger scenario in a job interview might look like this: Imagine you pass the interview. The company updates your candidate record to 'Passed,' and immediately, you receive a congratulatory email. At the same time, an entry is created in their system to start your background checks and onboarding process. All of this happens automatically right after you pass the interview.
The key thing to remember is that 'after' triggers run only after the record is saved. Once the record is saved, it generates the record Id, which you can use to reference and set up related records.
When do you use after trigger
1. Accessing the record id's
After triggers are useful when you need to create related child records. For example, when you submit a job application with your resume, the job application is created first, and then the resume is attached to it. The job application’s record id is used to link the resume to the application.
2. Updating Related Records
After triggers can be used to update related records. For example, in our job application scenario, once all background checks are completed, the job application status is automatically updated to 'Ready to Offer.'
The key point to remember is that if you need to update related records in a trigger scenario, you should use an 'after' trigger.
3. Sending notifications
Apex triggers can be used to send notifications, but these should only be sent in the 'after' context. For example, when the job status is updated from 'Ready to Offer' to 'Offered,' the system can automatically send an email notification to the candidate.
- Review interview feedback
- Check for additional documents
- Notify candidate about passing the interview
- Verify employment documents
- Complete background check
- Prepare the offer letter
- Coordinate with finance for salary package
- Set up candidate’s start date
- Send the offer letter to the candidate
- Follow up on candidate's decision
Apex Trigger Order of Execution
Loads the original record from the database or initializes the record for insert.
Loads the new record field values from the request and overwrites the old values.
Executes all before triggers.
Runs system validation rules.
Saves the record to the database but doesn't commit yet.
Executes all after triggers.