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
Belcy YBelcy Y 

write a 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 a trigger that when a country(custom field) of account(standard object) is updated, the country(custom field) of contact(standard object) should be copied
Soyab HussainSoyab Hussain

Here is an Apex trigger that implements the described behavior:
trigger UpdateContactCountry on Account (after update) {
  Set<Id> accountIds = new Set<Id>();
  Map<Id, String> accountCountryMap = new Map<Id, String>();

  // Collect the Ids of all updated accounts and their corresponding Country__c values
  for (Account a : Trigger.new) {
    if (a.Country__c != Trigger.oldMap.get(a.Id).Country__c) {
      accountIds.add(a.Id);
      accountCountryMap.put(a.Id, a.Country__c);
    }
  }

  // Get a list of all affected contacts
  List<Contact> contactsToUpdate = [SELECT Id, AccountId, Country__c FROM Contact WHERE AccountId IN :accountIds];

  // Update the Country__c field on all affected contacts
  for (Contact c : contactsToUpdate) {
    c.Country__c = accountCountryMap.get(c.AccountId);
  }
  update contactsToUpdate;
}