List < Contact > listContact = new List< Contact >();
for ( Account acct : trigger.new ) {
/* Checking whether Account’s Billing Street or City Changed */
if ( acct.BillingStreet != trigger.oldMap.get( acct.Id ).BillingStreet ||
acct.BillingCity != trigger.oldMap.get( acct.Id ).BillingCity )
mapAccount.put( acct.Id, acct );
}
if ( mapAccount.size() > 0 ) {
/* Fetching all the contacts related to the update accounts */
listContact = [ SELECT MailingStreet, MailingCity, AccountId FROM Contact WHERE AccountId IN : mapAccount.keySet() ];
if ( listContact.size() > 0 ) {
for ( Contact con : listContact ) {
/* Updating Contact’s Mailing Street and City from Account’s Billing Street and City */
con.MailingStreet = mapAccount.get( con.AccountId ).BillingStreet;
con.MailingCity = mapAccount.get( con.AccountId ).BillingCity;
}
update listContact;
}
}
}
tqq………..your post..
perfect
Thank you so much . Its really helpful !
Can you please update for same object. Example when updating parent account, related child accounts needs to be updated.
For Assets, it's possible with RootAssetId. For account, it is if you have fetch only child accounts(one level only).
How to implement this trigger for after insert. I want child record to be updated with another parent record if field value does not matches with current parent record
Please suggest
Use after insert event.
Get the child records using SOQL and update it accordingly by iterating it inside a for loop. Use a list variable to add the changes and update it after the for loop.
This comment has been removed by the author.
You cannot use Policy__r.District_Acct_Code__c and Account_Transfer__r.District_From__c without querying it. So, you are getting null pointer exception.
Is this code bulkified?
Yes. No DML or SOQL inside for loop.
What will happen if any contact update failed, will it revert the changes on account ?
Yes. I haven't used partial DML here.
Thank You so Much, This is very useful
Very useful. Can you please share code on rollup summary trigger for look up relationship
Check this – https://www.infallibletechie.com/2013/09/trigger-to-count-number-of-contacts.html
Could you please help me on below senario:
Account date field update based on latest contact(Which is created newly) date.
Thanks
Check this – https://www.infallibletechie.com/2021/11/trigger-to-update-account-when-contact.html
Thank you very much for your quick response.
But my requirment is Ex:Account A having 2 contacts.while creating contact user will give date(Field:NextExpiration) like 1st contact created 01/Aug/2021 and NextExpiration is 01/Nov/2021 and 2nd contact created 02/Nov/2021 and NextExpiration is 28/Nov/2021 so now we need to populate the NextExpiration(which is newly created contact NextExpiration date like 28/Nov/2021.)
In the same example, instead of using Date.Today(), you have to set the next expiration date value.