Write a trigger to update parent account phone number whenever the contact phone number is updated using trigger handler and helper class in Salesforce | How to update Account phone from Contact Phone based on lookup relationship in Salesforce

8,798 views


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.

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

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
   

trigger to update account phone with contact phone -- w3web.net

 

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]

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  1.    TRIGGER contactTrigger ON Contact (BEFORE INSERT, BEFORE UPDATE, BEFORE DELETE, after INSERT, after UPDATE, after DELETE, after undelete) {
  2.  
  3.     IF(TRIGGER.isBefore ){
  4.         system.debug('I am before trigger ');
  5.     }
  6.     ELSE IF(TRIGGER.isAfter){
  7.         system.debug('I am after trigger ');
  8.         IF(TRIGGER.isUpdate){
  9.             contactTriggerHandler.afterUpdateHelper(TRIGGER.new);
  10.         }
  11.     }
  12.  
  13.  
  14. }

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]

  1.    public class contactTriggerHandler {
  2.  
  3.     public static void afterUpdateHelper(List<Contact> conList){
  4.  
  5.          Set<Id> setId = NEW Set<Id>();
  6.         FOR(Contact con:conList){
  7.             setId.add(con.AccountId);
  8.         }
  9.         system.debug('setId ' + setId);
  10.  
  11.         List<Account> accItemList = [SELECT Id, Name, Phone, (SELECT Id, FirstName, LastName, Phone, AccountId FROM Contacts) FROM Account WHERE Id IN:setId];             
  12.         FOR(Account a1:accItemList){
  13.             FOR(Contact c1:a1.Contacts){
  14.                 IF(c1.Phone !=NULL){
  15.                      a1.Phone = c1.Phone;
  16.                      UPDATE accItemList;                     
  17.                 }
  18.             }
  19.         }
  20.  
  21.  
  22.     }
  23.  
  24.  
  25. }

trigger to update account phone with contact phone -- w3web.net

Further post that would you like to learn in Salesforce

 

 

 

FAQ (Frequently Asked Questions)

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

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

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



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment