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 - Contacts other Phone as OldPhone of Account - Contacts Mobile Phone as NewPhone of Account

Best Answer chosen by nilesh walke
Suraj Tripathi 47Suraj Tripathi 47

Nilesh,

You can do it like this.

trigger AccountTrigger on Account (after update,before update,before insert,after Insert,After Delete,before delete) {
    // Id record=Schema.SObjectType.Object__c.RecordTypeInfosByName.get('Student').RecordTypeId;
    
    if(Trigger.isAfter && trigger.isUpdate) {
        List<Contact> contactList=new List<Contact>([select id,OtherPhone,AccountId,MobilePhone from Contact where accountid in: trigger.new]);
       system.debug('contactList::'+contactList);
        for(Contact con:contactList){
            if(trigger.OldMap.get(con.AccountId).Phone!=null && trigger.NewMap.get(con.AccountId).Phone!=null){
                 con.OtherPhone=trigger.OldMap.get(con.AccountId).Phone;
                 con.MobilePhone=trigger.NewMap.get(con.AccountId).Phone;
            }
           
        }
        Update contactList;
    }else if(Trigger.isBefore && trigger.isDelete){
   List<Contact> contactList=new List<Contact>([select id,OtherPhone,AccountId,MobilePhone from Contact where AccountId in: trigger.old]);
    Account ac=[select id from Account where id='0012y00000DYSYJAA5'];
        for(Contact con:contactList){
            con.AccountId=ac.id;      
        }
        Update contactList;
    }
     
}
 

 

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi nilesh

Trigger:-
trigger updateAccount on Account (before update) {
    if(trigger.isUpdate && trigger.isBefore){
        helperAccount.method(trigger.old,trigger.new);
    }
}

apex:-
public class helperAccount{
    public static void method(List<account> oldAccount, List<account> newAccount){
        List<contact> conList=[select lastname,otherphone,accountid,mobilePhone from contact 
                               where accountid IN: newAccount];
        map<id,string> oldAccidVsPhone= new map<id,string>();
        map<id,string> newAccidVsPhone= new map<id,string>();
        for(account newAcc: newAccount){
            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;
        }
    }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

Thanks and Regards
Suraj Tripathi.
Suraj Tripathi 47Suraj Tripathi 47

Hi Nilesh,

Actually I have added One extra condition i.e when you are updating the Account and if there is a blank in phone then what will happen.

so Please note updated code.

trigger AccountTrigger on Account (after update) {
   
    if(Trigger.isAfter && trigger.isUpdate) {
        List<Contact> contactList=new List<Contact>([select id,OtherPhone,AccountId,MobilePhone from Contact where accountid in: trigger.new]);
         for(Contact con:contactList){
            if(trigger.OldMap.get(con.AccountId).Phone!=null && trigger.NewMap.get(con.AccountId).Phone!=null){
                 con.OtherPhone=trigger.OldMap.get(con.AccountId).Phone;
                 con.MobilePhone=trigger.NewMap.get(con.AccountId).Phone;
            } 
        }
        Update contactList;
        }
}

Can you please mark it as the Best Answer so that other people would take reference from it .

Thank You so much

 

nilesh walkenilesh walke
Thanks Suraj  i am suffering from 3 hours thanks for help  can you please help me in one more question 
here it  is . If you delete any account record corresponding child contact records to be assigned to a Particular account.
(Associate Contact record to another Account)
Thanks in Advance




    
Suraj Tripathi 47Suraj Tripathi 47

Your requirement is when an Account is deleted then their child linked to another account.

Yeah, I can do it but After deleting the account their child would be linked to which account?

nilesh walkenilesh walke
 hi Suraj
Any account we can assing by id  any specifit account
Suraj Tripathi 47Suraj Tripathi 47

Nilesh,

You can do it like this.

trigger AccountTrigger on Account (after update,before update,before insert,after Insert,After Delete,before delete) {
    // Id record=Schema.SObjectType.Object__c.RecordTypeInfosByName.get('Student').RecordTypeId;
    
    if(Trigger.isAfter && trigger.isUpdate) {
        List<Contact> contactList=new List<Contact>([select id,OtherPhone,AccountId,MobilePhone from Contact where accountid in: trigger.new]);
       system.debug('contactList::'+contactList);
        for(Contact con:contactList){
            if(trigger.OldMap.get(con.AccountId).Phone!=null && trigger.NewMap.get(con.AccountId).Phone!=null){
                 con.OtherPhone=trigger.OldMap.get(con.AccountId).Phone;
                 con.MobilePhone=trigger.NewMap.get(con.AccountId).Phone;
            }
           
        }
        Update contactList;
    }else if(Trigger.isBefore && trigger.isDelete){
   List<Contact> contactList=new List<Contact>([select id,OtherPhone,AccountId,MobilePhone from Contact where AccountId in: trigger.old]);
    Account ac=[select id from Account where id='0012y00000DYSYJAA5'];
        for(Contact con:contactList){
            con.AccountId=ac.id;      
        }
        Update contactList;
    }
     
}
 

 

This was selected as the best answer
Aman Pathak 18Aman Pathak 18
//Trigger
trigger AccountContactPhoneTrigger on Account (before update) {
    AccountContactPhoneTriggerHelper.updatePhoneOfContact(Trigger.new, Trigger.oldMap);
}

//Helper Class
public class AccountContactPhoneTriggerHelper{
    public static void updatePhoneOfContact(List<Account> newAccountList,Map<Id,Account> oldAccountMap){
        List <Contact> updatedContactList = new List<Contact>();
        for(Account acc : newAccountList){
            if(acc.Phone != oldAccountMap.get(acc.Id).Phone){
                List<Contact> contactList = [Select Phone, AccountId From Contact WHERE AccountId IN : oldAccountMap.keySet()];
                for(Contact con : contactList){
                    if(con.AccountId == acc.Id){
                        con.NewPhone = acc.Phone;
                        con.OldPhone =  oldAccountMap.get(acc.Id).Phone;
                        updatedContactList.add(con);
                    }
                }
            }
        }
        update updatedContactList;
    }
}