Write a trigger on contact to update parent record when child is updated using apex trigger in Salesforce | How to write trigger to update account Phone when contact Phone is updated in Salesforce

3,011 views

Hey guys, today in this post we are going to learn about how to write trigger to update account Phone when contact Phone is updated in Salesforce.

A trigger is the piece of code that executed before and after a record is Inserted/Updated/Deleted from the force.com database. Apex can be invoked through the use of triggers. A Trigger is a functional action which gets on particular events. Triggers will happen before records entering into the database and while goint out of the database. To know more details about Apex Trigger, Click Here.

 

 

Final Output β†’

how to update parent records when child record is updated -- 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 β–Ύ

 

Create Apex Trigger β†’

Step 1:- Create Apex Trigger : contactToAccountUpdateTrigger.apxt

From Developer Console >> File >> New >> Apex Trigger

contactToAccountUpdateTrigger.apxt [Apex Trigger]

  1.    TRIGGER contactToAccountUpdateTrigger ON Contact (BEFORE UPDATE, after UPDATE) {
  2.  
  3.     IF(TRIGGER.isBefore){
  4.         system.debug('Event before trigger');
  5.     }
  6.     ELSE IF(TRIGGER.isAfter){
  7.         system.debug('Event after trigger');
  8.         IF(TRIGGER.isUpdate){            
  9.             Map<Id,Contact> conMapItem=NEW Map<Id,Contact>();
  10.  
  11.             FOR(Contact con:TRIGGER.new){
  12.                 conMapItem.put(con.AccountId,con);        
  13.             }
  14.             List<Account> accObjList=[SELECT Id,Name,Phone,(SELECT Id,AccountId,Name,Phone FROM Contacts) FROM Account WHERE Id IN:conMapItem.keyset()];
  15.             IF(accObjList.size()>0){
  16.                 FOR(Account a:accObjList){
  17.                     a.Phone=conMapItem.get(a.id).Phone;            
  18.                 }
  19.                 UPDATE accObjList;        
  20.             }
  21.         }
  22.     }
  23.  
  24. }

 

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

Can we update record in after trigger?

Yes, you can update records in an after trigger - but you need to give it some thought and make sure it's the right thing to do.

What is trigger oldMap?

A map of IDs to the old versions of the sObject records. Note that this map is only available in the update and delete triggers.

What is the difference between after and before trigger?

Before Trigger is a type of trigger that automatically executes before a certain operation occurs on the table. In contrast, after trigger is a type of trigger that automatically executes after a certain operation occurs on the table.

Related Topics | You May Also Like

  • Our Free Courses β†’
 
 


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