
Apex Trigger In Salesforce - Get Started
The trigger is an apex code that executes before and after the DML operations.
Following are the DML operations:-
- Insert
- Update
- Delete
- Upsert
- Merge
- Undelete
The trigger is divided into two types:
- Before Trigger:- These triggers are used to update or validate record values before they are saved to the database.
- After Trigger:-These triggers are usually used to update or validate the value of records after they are saved to the database.
Types of events in the trigger:
- Before Insert
- Before Update
- Before delete
- After Insert
- After Update
- After Delete
- After Undelete
Don't forget to check out: Concept of Virtual and Abstract Modifiers in Apex | Salesforce Developer Guide
Note:- We can not use ‘Before Undelete’ because in this situation we don’t have a record id in the database.
Let us take an example:-
Example 1:- Add TASk to the Opportunity whenever a new opportunity is created or updated with Stage name = Closed won.
Step 1:- Login into your Salesforce org. And open your developer console.
Step 2:- Create a new Apex Trigger on Opportunity having name AddTaskToOpportunity.
Step 3:- Paste the below code in your trigger page and save it.
trigger AddTaskToOpportunity on Opportunity (after insert,after update) { List<task> tasks=New List<task>(); for(opportunity opp:trigger.new){ if(opp.stagename=='closed won'){ task t=new task(whatid=opp.id); tasks.add(t); } } upsert tasks; }
Now, you can check if the trigger is working fine or not just by creating a new opportunity and updating any record with Stage name = Closed Won.
Example 2:- Update the Name of the account with Phone field value as the suffix of the account name (Account Name =Name+Phone) whenever the new account is created or updated.
Step 1:- Login into your Salesforce org. And open your developer console.
Step 2:- Create a new Apex Trigger on Account having the name UpdateNameWithPhone.
Step 3:- Paste the below code into your trigger page and save it.
trigger UpdateNameWithPhone on Account (before insert, before update) { List<String> accountNames = new List<String>{}; for(Account a: Trigger.new){ a.Name =a.Name+' '+ a.Phone; } }
Check out another amazing blog by Anshu here: Learn About List and Set in Salesforce
Now, you can check if the trigger is working fine or not just by creating and updating an account.
Examle 3:- Send a mail to the current user whenever an account is created or updated with Rating = Hot.
Step 1:- Login into your Salesforce org. And open your developer console.
Step 2:- Create a new Apex Trigger on Account having name emailRating.
Step 3:- Paste the below code into your trigger page and save it.
trigger emailRating on Account(After insert,After update) { For(Account acc:Trigger.new ){ if(acc.Rating == 'Hot'){ User curnt_user=[SELECT Email FROM User WHERE Id= :UserInfo.getUserId()] ; Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {curnt_user.Email}; mail.setToAddresses(toAddresses); mail.setSubject('Account Rating'); mail.setPlainTextBody('Rating equals to hot'); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail}) ; } } }
Now, you can check if the trigger is working fine or not just by creating and updating an account with Rating = Hot. And then you can check your mailbox.
Responses