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]
TRIGGER myAccountTrigger ON Account (BEFORE UPDATE, after UPDATE) {
IF(TRIGGER.isBefore){
}ELSE IF(TRIGGER.isAfter && TRIGGER.isUpdate){
Map<Id, Account> accObjMap = NEW Map<Id, Account>();
FOR(Account acc:TRIGGER.new){
accObjMap.put(acc.Id, acc);
}
List<Contact> conObjList = NEW List<Contact>();
List<Account> accObjList = [SELECT Id, Name, Email__c,(SELECT Id, Name, FirstName, LastName, AccountId FROM Contacts) FROM Account WHERE Id IN:accObjMap.keySet()];
List<Contact> conItem = NEW List<Contact>();
FOR(Account acc2:accObjList){
conObjList = acc2.Contacts;
}
FOR(Contact con:conObjList){
con.Email = accObjMap.get(con.AccountId).Email__c;
conItem.add(con);
}
IF(!conItem.isEmpty()){
UPDATE conItem;
}
}
}
Further post that would you like to learn in Salesforce
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 β
π 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 |