Salesforce provides ‘Cross Object Formulae’ to get data from the master record and use it in a master-detail record. Here my requirement is to update a field in a master record when there is a change in master-detail record. This can be done with the help of a concept called ‘Trigger’ in Salesforce.
A Trigger is an Apex code which executes before or after inserting or modifying a record based on the condition provided. There are different types of triggers based on the action going to be performed. They are Before Triggers and After Triggers. Triggers allow modification of another record of the same type or different type.
Apex Trigger explained with an example
Consider an example of account and contact in which account is having a field called ‘Field_Update__c’ which needs to be updated when a new contact is created or existing contact is updated. This can be achieved through triggers.
Below are the steps to be followed:
- Create a field in ‘Account’ with label ‘Field Update’ and data type as ‘Checkbox’
- Now create a trigger on Contact
- Navigate to Setup ->Build ->Customize ->Contacts ->Triggers
- Click on ‘New’ Button, below screen will be shown
trigger <name> on Contact (<events>) { }
- Now, write the ‘Apex code’ to update the trigger. Below is the code to update the field in ‘Account’
The below code is an Apex code to update felid.
Trigger AccountUpdate on Contact (after insert, after update) { Set <String> accID = New Set <String> (); For (Contact con: Trigger.new) { if (con.AccountId != Null ) { accID.add (con.AccountId); } } If (accID.size ()> 0) { List <Account> upAccList = new List <Account> (); For (Account ac: [SELECT Id, Field_Update__c FROM Account WHERE id in: AccID AND Field_Update__c != True]) { ac.Field_Update__c = true; UpAccList.add (ac); } If (upAccList.size ()> 0) update upAccList; } }
Explanation of the APEX Code
- In line number 1, I’ve provided the trigger name as ‘AccountUpdate’ and the events are ‘after insert’ and ‘after update’. This trigger will be executed after creating a new record or updating an existing record.
- In line number 3, a set ‘accID’ is created to store ‘AccountIds’ of the updated contacts
- In line number 4, a ‘for loop’ is used to loop over the contacts which are inserted or updated
- In line number 5, we’re checking whether the Contact is having an ‘AccountId’ or not
- If the contact is having AccountId, it’ll be added to the set ‘accID’ in line number6
- We’re adding all the records to a set so that, all the records will be updated at a time to avoid hitting Salesforce Governor limits
- In line number 9 we’re checking if there are any AccountId’s existing in set ‘accID’
- If there are any Accounts which needs to be updated, retrieve those ‘Accounts’ using the formula given below
SELECT Id, Field_Update__c FROM Account WHERE id in: AccID AND Field_Update__c != True
- Here, we’re retrieving the records whose id is existing in the set ‘accID’ and Field_Update__c is false
- In line number10, value of Field_Update__c is set to true
- In line number11, it’s updated to the list ‘UpAccList’
- Now ‘UpAccList’ is having the list of Accounts which needs to be updated
- In line number15, we’re checking the size of ‘UpAccList’
- If the list ‘UpAccList’ is not empty, all the Accounts are updated using ‘update’ action as shown in line number16
-
update upAccList;
- Now, click on ‘Save’ button to save the trigger
- In this way, we can create a trigger on Contact to update a field on Account
- As the trigger is created, I’ll be testing this by creating a new contact and updating a contact
- Below screenshot shows the ‘Account’ is having one contact and ‘Field Update’ is false
- Now, I’ll be creating a new contact for this account so that, Field update checkbox will be updated
- In the below screen, you can see a new contact is created and ‘Field update’ checkbox is checked
- Now, let’s look into contact update
- Below screen shows an Account with a contact and ‘Field update’ is false
- Now, I’ve updated the contact name from ‘Jhon’ to ‘Jhon Ken’. The ‘Field update’ is set to true, which is shown as below
In this way, we can update the Account when it’s contacts are inserted or updated. Salesforce provides ‘Apex’ to handle the scenarios which can’t be implemented using standard functionality.
How we can help
Our team can help you customize and integrated Salesforce as per your business process. Read about our Salesforce Integration Services. Reach out to us for a free assessment of your business needs.