function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vethanbu LibertinVethanbu 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
Prateek Prasoon 25Prateek Prasoon 25
trigger CopyCountryFromAccountToContact on Account (after update) {
    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.