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
Nitin SharmaNitin Sharma 

How to update contact of a account to another account record.

Req.- On account object, having 2 fields 1st- D1 and 2nd- D2.
While inserting or updating account if D1 != D2 then it will compare the D2 value to D1 values from all org account and if any record found then that account having all contacts assigned to Inserting or updating an account.  
Hi Nitin 

I can do your Requirement with trigger 
Hi Nitin 

I am done with your trigger it's such a nice Problem I enjoy to solving this 
trigger UpdatingContactWhenFieldMatch on Account (after insert,after update) {
    Map<String,Id> allDataWithMap = new Map<String,Id>(); 
    Set<String> allD2VALUE = new Set<String>();    
    List<Contact> allContactListUpdate = new List<Contact>();
        if(acc.D1__c != NULL && acc.D2__c != NULL && !acc.D1__c.equals(acc.D2__c)){

     for(Account acc:[select id,name,D1__c, (select id,name,Accountid from contacts ) 
                                            from account where D1__c IN : allD2VALUE]){
                          if(acc.contacts != null && allDataWithMap.containsKey(acc.D1__c)){
                                 for(Contact con:acc.contacts){
                                    Contact cc = new contact();
                                    cc.Id = con.Id;
                                    cc.AccountId = allDataWithMap.get(acc.D1__c);
        update allContactListUpdate;


if this was helpful don't forget to mark as best answer else let me know your problem 

Thank you 
Avaneesh Singh
Hi Nitin,
I hope this code will help you to get your required answer in simplest way
// Apex Code for Trigger //
public with sharing class AccountContact_D1_D2
   public static void toUpdateField(list<Account> acList)
       list<String> strList = new list<String>();
       set<ID> acId = new set<ID>();
       for(Account a : acList)
           if(a.D1__c != a.D2__c) // Contion to check D1 is Not Equal to D2
               strList.add(a.D2__c); // Adding D2 values in strList
       if(strList.size() > 0) {
           list<Account> accListWithD2 = [select Id, Name, D1__c from Account where D1__c IN: strList];
// Getting all the Accounts from the org where D1 is Equal to D2 of Current Account which creating now
           set<ID> newAcId = new set<ID>();
           for(Account ac : accListWithD2) {  // Iteration of account to Get their IDs
           if(newAcId.size() > 0) // If newAcId if Not Null
               list<Contact> conList = [select Id, LastName, AccountId from Contact where AccountId IN: newAcId];
// Getting the all Contacts Associated with that Account where Id in newAcId

               list<Contact> newConList = new list<Contact>();
               map<Id, list<Contact>> accConMap = new map<Id, list<Contact>>();
               for(Account a1 : acList) {
                   for(Contact c : conList) {
                       Contact con = new Contact();
                       con.LastName = c.LastName;
                       con.AccountId = a1.Id;
               insert newConList;
Code for Trigger:
trigger AccountContact_D1_D2_Trigger on Account (after insert, after update)
If this helps you then make it as Best.
With Regards,
Saket Sharma 24Saket Sharma 24
Hi Nitin your requirement can be done using trigger which is below
trigger DOneDTwoTrigger on Account (after insert,after update) {
    Map<Decimal,List<Id>> allData=new Map<Decimal,List<Id>>();
    for(Account acc:
            List<Id> toPut;
            else toPut=new List<id>();
            allData.put(acc.D2__c, toPut);
    List<Account> allAccount=new List<Account>();
    allAccount=[select name,D1__c,D2__c,(select name,id,AccountId from Contacts) from Account where D1__c in : allData.keySet()];
    List<Contact> toUpdate=new List<Contact>();
    for(Account acc:allAccount)
        for(Contact c: acc.Contacts){
            c.AccountId=allData.get(acc.D1__c)[0];//for multiple Account lets choose the first one.
            System.debug('Account Id'+allData.get(acc.D1__c)[0]);
        update toUpdate;