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.  
AvaneeshAvaneesh
Hi Nitin 

I can do your Requirement with trigger 
AvaneeshAvaneesh
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>();
    for(Account acc:trigger.new){
        if(acc.D1__c != NULL && acc.D2__c != NULL && !acc.D1__c.equals(acc.D2__c)){
            allD2VALUE.add(acc.D2__c);
            allDataWithMap.put(acc.D2__c,acc.Id);
        }
    }

    if(allD2VALUE.size()>0){
     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);
                                    allContactListUpdate.add(cc);
                                   }
                                }            
                            }
     }                       
    if(allContactListUpdate.size()>0){
        update allContactListUpdate;
       }               
}

Cheer's..............

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

Thank you 
Avaneesh Singh
Akshay_DhimanAkshay_Dhiman
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
               newAcId.add(ac.Id);
           }
           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;
                       newConList.add(con);
                   }
               }
               insert newConList;
           }
       }
   }
}
Code for Trigger:
trigger AccountContact_D1_D2_Trigger on Account (after insert, after update)
{
AccountContact_D1_D2.toUpdateField(trigger.new);
}
If this helps you then make it as Best.
Thankyou,
With Regards,
Akshay
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: trigger.new)
    {
        if(acc.D1__c!=acc.D2__c){
            List<Id> toPut;
            if(allData.containsKey(acc.D2__c))
                toPut=allData.get(acc.D2__c);
            else toPut=new List<id>();
            toPut.add(acc.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.
            toUpdate.add(c);
            System.debug('Account Id'+allData.get(acc.D1__c)[0]);
        }
    }
    if(toUpdate.size()>0)
        update toUpdate;