How to update child records when parent record is updated in Salesforce?

Sample trigger:

trigger ContactUpdate on Account (after update) {
    set<Id> acctIds = new set<Id>();
    map<Id, Account> mapAccount = new map<Id, Account>();
    list<Contact> listContact = new list<Contact>();
    for(Account acct : {
        mapAccount.put(acct.Id, acct);
    listContact = [SELECT MailingStreet, MailingCity, AccountId FROM Contact WHERE AccountId IN : acctIds];
    if(listContact.size() > 0) {
        for(Contact con : listContact) {
            con.MailingStreet = mapAccount.get(con.AccountId).BillingStreet;
            con.MailingCity = mapAccount.get(con.AccountId).BillingCity;
        update listContact;


No comments:

Post a Comment

Sharing is Caring!!! Share this with your friends!!!

submit to reddit