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
nilesh walkenilesh walke 

Whenever an account phone is modified, update all the contacts of the account

public static void ForQnoSIX(List<account> oldAccount, List<account> Ac4){
        List<contact> conList=[select lastname,otherphone,accountid,mobilePhone from contact 
                               where accountid IN: Ac4];
        map<id,string> oldAccidVsPhone= new map<id,string>();
        map<id,string> newAccidVsPhone= new map<id,string>();
        for(account newAcc: Ac4){
            for(account oldAcc: oldAccount){
                if(newAcc.phone!=oldAcc.phone && oldAcc.id==newAcc.id){
                    oldAccidVsPhone.put(oldAcc.id,oldAcc.phone);
                    newAccidVsPhone.put(newAcc.id,newAcc.phone);
                }
            }
        }
        list<contact> updateContactList= new List<contact>();
        for(contact con: conList){
            if(oldAccidVsPhone.containskey(con.accountid)){
                con.otherphone=oldAccidVsPhone.get(con.accountid);
                con.mobilePhone=newAccidVsPhone.get(con.accountid);
                updateContactList.add(con);
            }
        }
        if(updateContactList.size()>0){
            update updateContactList;
        }
    } 


i need test class for it 
thanks for helping me out 
stayinpgsz stayinpgszstayinpgsz stayinpgsz
 The head of the school is likewise cordial and guide us completely. There is likewise a situation cell in the school. There are various flasks in the school. The confirmation system is basic. I best pg near law faculty favored this school over others on the grounds that the educators here are overall quite well disposed and furthermore M.Sc. Plant science is available in this school or in GNDU.
 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Nilesh,

You can try the below snippet:
 
trigger AccountTrigger on Account (after update) {
    if(Trigger.isAfter && trigger.isUpdate) { 
Classname.ForQnoSIX(trigger.oldmap, trigger.newmap)
}
    }
 
public static void ForQnoSIX(Map<id,account> oldAccountmap, Map<id,account> Ac4){
set<id> idset= Ac4.keyset();
        List<contact> conList=[select lastname,otherphone,accountid,mobilePhone from contact 
                               where accountid IN: idset];

      list<contact> updateContactList= new List<contact>();
     
   for(contact con: conList){
Account OldAccount = oldAccountmap.get(con.AccountId);
Account NewAccount= Ac4.get(con.AccountId);
            if(NewAccount.phone!=OldAccount.phone){
                con.otherphone=OldAccount.phone;
                con.mobilePhone=NewAccount.phone;
                updateContactList.add(con);
            }
        }
        if(updateContactList.size()>0){
            update updateContactList;
        }
    }
 
@isTest()
public class ForQnoSIXTest
{
    private static testMethod void testUpdateCaseMethod() 
	{
        Account a= new account();
        a.name='test';
        insert a;
        
        Contact c= new contact();
        c.AccountId= a.id;
        c.phone =56789123;
        insert c;
        
        Account Temp= new Account();
        Temp.id= a.id;
        Temp.phone=123456789;
        update Temp;

        Contact c = [select id,phone from contact where id in :c.id];

system.assertNotEquals(123456789, c.phone, 'The value of phone on contact should same as account.');
    }
    
}

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
CharuDuttCharuDutt
Hii Nilesh Walke
Try Below Code Made A Small Change In Your Class
public static void ForQnoSIX(List<account> oldAccount, List<account> Ac4){
        List<contact> conList=[select lastname,otherphone,accountid,mobilePhone from contact 
                               where accountid IN: Ac4];
        map<id,string> oldAccidVsPhone= new map<id,string>();
        map<id,string> newAccidVsPhone= new map<id,string>();
        for(account newAcc: Ac4){
            for(account oldAcc: oldAccount){
                if(newAcc.phone!=oldAcc.phone/* && oldAcc.id==newAcc.id*/){
                    oldAccidVsPhone.put(oldAcc.id,oldAcc.phone);
                    newAccidVsPhone.put(newAcc.id,newAcc.phone);
                }
            }
        }
        list<contact> updateContactList= new List<contact>();
        for(contact con: conList){
            if(oldAccidVsPhone.containskey(con.accountid)){
                con.otherphone=oldAccidVsPhone.get(con.accountid);
                con.mobilePhone=newAccidVsPhone.get(con.accountid);
                updateContactList.add(con);
            }
        }
        if(updateContactList.size()>0){
            update updateContactList;
        }
    } 




Test Class:
@isTest
public class unitest {
  @isTest
    public static void unittest(){
       integer k=0;
       list<Contact>lstCon = new list<Contact>();
        list<Account>lstAcc = new list<Account>();
        list<Account>UpdatelstAcc = new list<Account>();
        For(integer i=0;i<5;i++){
            Account acc = new Account();
            acc.Name = 'testAcc ' + i;
            acc.Phone = '123456789'+i;
            lstAcc.add(acc);
        }
        insert lstAcc;
        
        For(integer i=0;i<5;i++){
            Contact Con = new Contact();
            con.LastName = 'testAcc ' + i;
            Con.Phone = '7654389321'+i;
            Con.otherphone = '7654389321'+i;
            Con.mobilephone = '7654389321'+i;
            for(integer z=0;z<lstAcc.Size();z++){
            Con.AccountId = lstAcc[z].id;
                }
            lstCon.add(Con);
        }
        insert lstCon;
        for(Account updateAcc: lstAcc){
            if(updateAcc.Id == lstAcc[k].Id){
            updateAcc.Phone ='123654789'+k++;
            }
            UpdatelstAcc.Add(updateAcc);
        }
        update UpdatelstAcc;
        
        testClass.ForQnoSIX(lstAcc, UpdatelstAcc);
    }

}
Please Mark It As Best Answer If It Helps
 Thank You!