Apex trigger is a piece of code which executes when an event ocurrs. It executes when a record is Inserted,Deleted or updated from the force.com database.
Syntax:
trigger <NameOfTrigger> on ObjectName (trigger_events) { //what trigger can do }
These are the events on which trigger get fires:
- Insert
- Update
- Delete
- Merge
- upsert
- undelete
Let’s take an example:
Suppose we have a requirement in which we need to create a new Invoice record whenever Customer Status field is changed from Inactive to active status.
So now we need to create a trigger on which object ? APEX_Customer__c
Step 1: Login to Salesforce.com
Step 2: Go to the custom object “Customer”
Step 3: Click on New button in Trigger related list section.
//Trigger Code trigger CustomerTrigger on APEX_Customer__c (after update) { List InvoiceList = new List(); for (APEX_Customer__c customerObj: Trigger.new) { if (customerObj.APEX_Customer_Status__c == 'Active') { APEX_Invoice__c invoideObj = new APEX_Invoice__c(); invoideObj.APEX_Status__c = 'Pending'; InvoiceList.add(invoideObj); } } //DML to insert the Invoice List in SFDC insert InvoiceList; }
Let me explain you the code step by step:
In the first line, we are creating a trigger on our custom object and it tells you to run the trigger after the record is updated. Then we are creating a list and named it as InvoiceList. In the third line,you have encountered Trigger.new.
Trigger.new: This is the context variable which keep track of the records which are currently in context of trigger either they are being inserted or updated. So In this example,customerObj variable has all the records which has been updated.
Check out the complete list of context variables here:
So now you are familiar with Trigger.new. So customerObj variable has list of records which are updated,it can be one or more. So we have created a for loop that goes through the records one by one and for each record, it checks the customer_status field. If the customer_status is updated to Active then it create a new Invoice Object and assign the status to Pending.
Once he has gone through the list of records, he finally exits the for loop and runs a DML query to insert all the invoice records in the database.
Let’s take another example to make things more clearer:
The above trigger will execute everytime the Customer records are updated.
Let’s say we only want to insert invoice records when Customer status changes from Inactive to Active, which means we also need to see what was the previous customer status and what’s the new one. If the previous was Inactive and the new one is Active then only we will create a new Invoice record. So in this case, we will use Trigger.oldMap
As per the definition of trigger.oldMap: It’s a map of Ids to the old versions of the sObject records.
//Modified Trigger Code trigger CustomerTrigger on APEX_Customer__c (after update) { List<apex_invoice__c> InvoiceList = new List<apex_invoice__c>(); for (APEX_Customer__c customerObj: Trigger.new) { //condition to check the old value and new value if (customerObj.APEX_Customer_Status__c == 'Active' && trigger.oldMap.get(customerObj.id).APEX_Customer_Status__c == 'Inactive') { APEX_Invoice__c invoiceObj = new APEX_Invoice__c(); invoiceObj.APEX_Status__c = 'Pending'; InvoiceList.add(invoiceObj); } } //Dml to insert the invoice records insert InvoiceList; }
I also have a online course on Salesforce Development which covers triggers in detail so, if you are interested. Check the course here