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
Muni12345Muni12345 

If we update the Account object description its automatically need to update contact description by using map collection?

Sample program for If we update the Account object description its automatically need to update contact description by using map collection.

Please post the solution,

Thanks in advance
Best Answer chosen by Muni12345
SandhyaSandhya (Salesforce Developers) 
Hi,

Please refer below sample code.
 
trigger Crossobjectfieldupdate on Account (after update) 
{
  if (Trigger.isUpdate)
  {
      Map<Id, Account> updAccs=new Map<Id, Account>();
      for (Account acc : trigger.new)
      {
         Account oldAcc=trigger.oldMap.get(acc.id);
         if (acc.Test_Date__c != oldAcc.Test_Date__c)
         {
            updAccs.put(acc.id, acc);
         }
       }
 
       List<Contact> updCon=[select id, accountId, Test_Date__c from contact where accountId in :updAccs.keySet()];
       for (Contact con: updCon)
       {
           Account acc=updAccs.get(con.accountId);
           con.Test_Date__c=acc.Test_Date__c;
       } 
   }
 
   update updCon;
}

Hope this helps you!

Please mark it as Best Answer if my reply was helpful. It will make it available for other as the proper solution.
 
Thanks and Regards
Sandhya

 

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

Please refer below sample code.
 
trigger Crossobjectfieldupdate on Account (after update) 
{
  if (Trigger.isUpdate)
  {
      Map<Id, Account> updAccs=new Map<Id, Account>();
      for (Account acc : trigger.new)
      {
         Account oldAcc=trigger.oldMap.get(acc.id);
         if (acc.Test_Date__c != oldAcc.Test_Date__c)
         {
            updAccs.put(acc.id, acc);
         }
       }
 
       List<Contact> updCon=[select id, accountId, Test_Date__c from contact where accountId in :updAccs.keySet()];
       for (Contact con: updCon)
       {
           Account acc=updAccs.get(con.accountId);
           con.Test_Date__c=acc.Test_Date__c;
       } 
   }
 
   update updCon;
}

Hope this helps you!

Please mark it as Best Answer if my reply was helpful. It will make it available for other as the proper solution.
 
Thanks and Regards
Sandhya

 
This was selected as the best answer
Muni12345Muni12345
Thank you Sandhya, now i got it..