Hey guys, today in this post we are going to learn about How to Write a trigger to update parent account phone number when ever the contact phone number is updated using trigger handler and helper class in Salesforce
Real time scenarios:- Write a trigger on Contact to update the parent Account Phone number when ever the Contact Phone is updated through trigger handler and helper class in Salesforce.
- Don’t forget to check out:- Trigger to check duplicate name to custom object in Salesforce Click Here For More Information
Files we used in this post example
contactTrigger.apxt | Apex Class Trigger | It will be fired when ever contact phone number is updated. |
contactTriggerHandler.apxc | Apex Class Trigger Handler | Apex handler trigger to update account phone number with contact phone number when ever contact is updated. |
Parent Object:- Account Fields:- Phone Child Object:- Contact Fields:- Phone, AccountId (Lookup) |
Trigger to updated parent Account from Contact based on lookup relation AccountId. |
Final Output
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
- Find the below steps for this post.
Create Apex Class Trigger
Step 1:- Create Apex Class : contactTrigger.apxt
From Developer Console ➡ File ➡ New ➡ Apex Class
contactTrigger.apxt [Apex Class Controller]
TRIGGER contactTrigger ON Contact (BEFORE INSERT, BEFORE UPDATE, BEFORE DELETE, after INSERT, after UPDATE, after DELETE, after undelete) {
IF(TRIGGER.isBefore ){
system.debug('I am before trigger ');
}
ELSE IF(TRIGGER.isAfter){
system.debug('I am after trigger ');
IF(TRIGGER.isUpdate){
contactTriggerHandler.afterUpdateHelper(TRIGGER.new);
}
}
}
Create Apex Trigger Handler and Helper Class
Step 2:- Create Apex Class : contactTriggerHandler.apxc
From Developer Console ➡ File ➡ New ➡ Apex Class
contactTriggerHandler.apxc [Apex Class Controller]
public class contactTriggerHandler {
public static void afterUpdateHelper(List<Contact> conList){
Set<Id> setId = NEW Set<Id>();
FOR(Contact con:conList){
setId.add(con.AccountId);
}
system.debug('setId ' + setId);
List<Account> accItemList = [SELECT Id, Name, Phone, (SELECT Id, FirstName, LastName, Phone, AccountId FROM Contacts) FROM Account WHERE Id IN:setId];
FOR(Account a1:accItemList){
FOR(Contact c1:a1.Contacts){
IF(c1.Phone !=NULL){
a1.Phone = c1.Phone;
UPDATE accItemList;
}
}
}
}
}
Further post that would you like to learn in Salesforce
What is trigger in Salesforce?
A trigger is an Apex script that executes before or after data manipulation language (DML) events occur.
Why do we use triggers in Salesforce?
Typically, we use triggers to perform operations based on specific conditions, to modify related records or restrict certain operations from happening.
What are different events in trigger?
The events that fire a trigger include the following: DML statements that modify data in a table ( INSERT , UPDATE , or DELETE ) DDL statements.
Related Topics | You May Also Like
Our Free Courses →
👉 Get Free Course →
📌 Salesforce Administrators 📌 Salesforce Lightning Flow Builder 📌 Salesforce Record Trigger Flow Builder |
👉 Get Free Course →
📌 Aura Lightning Framework 📌 Lightning Web Component (LWC) 📌 Rest APIs Integration |