Update all Email of Contact related list to the associated Account if Account is updated in Salesforce | How to update all Contact record related to Account in Salesforce

133 views


Hey guys, today in this post we are going to learn about How to Update all Email of Contact related list to the associated Account if Account is updated in Salesforce.

 

Final Output →

 

Other related post that would you like to learn in Salesforce

 

Create Apex Trigger →

Step 1:- Create Apex Controller : myAccountTrigger.apxt

SFDX:Create Apex Trigger>> New >> myAccountTrigger.apxt

myAccountTrigger.apxt [Apex Trigger]

  1.   TRIGGER myAccountTrigger ON Account (BEFORE UPDATE, after UPDATE) {
  2.  
  3.     IF(TRIGGER.isBefore){
  4.  
  5.     }ELSE IF(TRIGGER.isAfter && TRIGGER.isUpdate){
  6.  
  7.         Map<Id, Account> accObjMap = NEW Map<Id, Account>();
  8.  
  9.         FOR(Account acc:TRIGGER.new){
  10.             accObjMap.put(acc.Id, acc);
  11.         }
  12.  
  13.         List<Contact> conObjList = NEW List<Contact>();
  14.         List<Account> accObjList = [SELECT Id, Name, Email__c,(SELECT Id, Name, FirstName, LastName, AccountId FROM Contacts) FROM Account WHERE Id IN:accObjMap.keySet()];
  15.         List<Contact> conItem = NEW List<Contact>();
  16.  
  17.         FOR(Account acc2:accObjList){
  18.             conObjList = acc2.Contacts;
  19.         }
  20.  
  21.         FOR(Contact con:conObjList){
  22.             con.Email = accObjMap.get(con.AccountId).Email__c;
  23.             conItem.add(con);
  24.         }
  25.  
  26.         IF(!conItem.isEmpty()){
  27.             UPDATE conItem;
  28.         }
  29.  
  30.     }
  31. }

Further post that would you like to learn in Salesforce

 

FAQ (Frequently Asked Questions)

What are the two options for when apex triggers can run?

There are two types of Apex triggers in Salesforce: Before Triggers and After Triggers. Before Triggers execute before the record is saved to the database, while After Triggers execute after the record has been saved.

Can apex trigger make a callout?

Even though there's the restriction of not allowing API callouts from triggers, there's a workaround to make it happen. You can use Asynchronous Apex to make callouts.

What is the limit of apex triggers in Salesforce?

Your Apex triggers (combined) must not exceed 200 SOQL queries per batch. If they do, your Clean job for that object fails. In addition, if your triggers call future methods, they're subject to a limit of 10 future calls per batch.

Related Topics | You May Also Like

Our Free Courses →

👉 Download App to Get Free Course →

📌 Salesforce Administrators

📌 Salesforce Lightning Flow Builder

📌 Salesforce Record Trigger Flow Builder

👉 Download App to Get Free Course →

📌 Aura Lightning Framework

📌 Lightning Web Component (LWC)

📌 Rest APIs Integration

 
 
🌩️ Download App to Get Free Course Access: -

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