You need to sign in to do that
Don't have an account?
Vethanbu Libertin
write trigger that when a country(custom field) of account(standard object) is updated, the country(custom field) of contact(standard object) should be copied
write trigger that when a country(custom field) of account(standard object) is updated, the country(custom field) of contact(standard object) should be copied
List<Contact> contactsToUpdate = new List<Contact>();
for (Account acc : Trigger.new) {
// Check if the country field has been updated
if (acc.Country__c != Trigger.oldMap.get(acc.Id).Country__c) {
// Get all Contacts associated with the updated Account
List<Contact> contacts = [SELECT Id, Country__c FROM Contact WHERE AccountId = :acc.Id];
// Update the country field of each Contact
for (Contact con : contacts) {
con.Country__c = acc.Country__c;
contactsToUpdate.add(con);
}
}
}
// Bulk update the Contacts
if (!contactsToUpdate.isEmpty()) {
update contactsToUpdate;
}
}
Note: In this code, the custom field Country__c is used on both Account and Contact objects. Please replace it with the actual field name if it is different in your org.
If you find my answer helpful, please mark it as the best answer.