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
Aishwarya P 4Aishwarya P 4 

If a user changes Phone in Account then its Contacts Phone should get updated with new value, only if the Contact Phone was same as account's old Phone value.

Please help me write a trigger and class for the same.
Best Answer chosen by Aishwarya P 4
Apoorv Saxena 4Apoorv Saxena 4
Hi Aishwarya,

Try this code :
 
trigger updateContactsPhone on Account (before update) {
   List<Account> accList;
   List<Contact> conList = new List<Contact>();
   Set<Id> accIdSet = new Set<Id>();
   
    for (Account acc : Trigger.New){
        if(trigger.oldMap.get(acc.Id).phone<>acc.phone){
            accIdSet.add(acc.id);
        }
    }
    if(accIdSet<>null && accIdSet.size()>0){
        accList = [Select id,phone,(Select id,Phone from Contacts) from Account where ID in:accIdSet];
    }
    
    for(Account acc:accList){
        for(Contact con:acc.contacts){
            if(acc.phone==con.phone){
                con.phone = trigger.newMap.get(acc.id).phone;
                conList.add(con);
            }
        }
    }
        
    if(conList<>null && conList.size()>0){     
        update conList;
    }
}

Please let me know how it works out for you.

Please mark this question as Solved if this answers your question so that others can view it as a proper solution.

Thanks,
Apoorv

All Answers

Aishwarya P 4Aishwarya P 4
@piyush_soni I think you got the question wrong.
 
Apoorv Saxena 4Apoorv Saxena 4
Hi Aishwarya,

Try this code :
 
trigger updateContactsPhone on Account (before update) {
   List<Account> accList;
   List<Contact> conList = new List<Contact>();
   Set<Id> accIdSet = new Set<Id>();
   
    for (Account acc : Trigger.New){
        if(trigger.oldMap.get(acc.Id).phone<>acc.phone){
            accIdSet.add(acc.id);
        }
    }
    if(accIdSet<>null && accIdSet.size()>0){
        accList = [Select id,phone,(Select id,Phone from Contacts) from Account where ID in:accIdSet];
    }
    
    for(Account acc:accList){
        for(Contact con:acc.contacts){
            if(acc.phone==con.phone){
                con.phone = trigger.newMap.get(acc.id).phone;
                conList.add(con);
            }
        }
    }
        
    if(conList<>null && conList.size()>0){     
        update conList;
    }
}

Please let me know how it works out for you.

Please mark this question as Solved if this answers your question so that others can view it as a proper solution.

Thanks,
Apoorv
This was selected as the best answer
Tejas KardileTejas Kardile
Hi Aishwarya,

You can achive this using process builder, please follow below steps:
  1. Create Process builder on Account
  2. Set Criteria - Formula evaluates to true and use below formula
  3. And( !(IsNew()),!( PRIORVALUE( [Account].Phone )= [Account].Phone))
  4. If formula True, Then write Field update action
  5. Select "Criteria for Updating Records*" as "Updated records meet all conditions"
  6. Write below value in Filter the records you update based on these conditions
  7. Mobile Phone --> Equals -->  Formuala -->  ( PRIORVALUE( [Account].Phone )
  8. Write Below value in "Set new field values for the records you update"
  9. Mobile Phone --> Reference --> [Account].Phone
With above solution you will be able to achieve your output without writing code.

Let me know if this solve your problem.

Thanks
sfdcMonkey.comsfdcMonkey.com
yes my missunderstanding  try this code
trigger ContactPhone on Account (after update) {
   set<id> setAccountId = new set<id>();
   List<contact> updateCon = new List<contact>();
   for(account oAccount:trigger.new){
       setAccountId.add(oAccount.id);
     }  
  
  Map<id,account> mapIdWiseAccount = new Map<id,account>();
   for(account acc:[select id,phone,(select id,phone from Contacts) from account where ID IN :setAccountID]){
     mapIDWiseAccount.put(acc.Id,acc);     
    }
    for(account a : trigger.new){
      account oAccount = mapIDWiseAccount.get(a.id); 
      String accPhone = trigger.oldMap.get(a.id).phone;
    if(accPhone != null){
    for(contact c : oAccount.Contacts){
      if(accPhone == c.phone)         
          c.Phone = a.phone;
          updateCon.add(c);  
        }
         
      }    
        }
      if(updateCon.size() > 0){  
        update updateCon;
         }
      }
Let me inform if it helps you
Thanks
 
Aishwarya P 4Aishwarya P 4
Thank you apoorv.It works now.