+ Start a Discussion
Deepu sfdcDeepu sfdc 

Trigger to update child records

Hi,

I want to update the contact phone field whenever account phone filed is updated .
Please share the code.

Thanks in advance
Best Answer chosen by Deepu sfdc
Ajay K DubediAjay K Dubedi
Hi Deepu,
The code is given below:

trigger tri on Account (after update) {
    
   tri_helper.acontPhon(trigger.new);
}

public class tri_helper {
    public static void acontPhon(list<Account> acList)
    {
       List<Contact> cList=new List<Contact>();
       
        for(Contact c:[SELECT Id,phone,account.phone From Contact Where AccountId IN:acList])
        {
            c.phone=c.Account.phone;
            cList.add(c);
            
        }
        if(cList.size()>0)
        update cList;

}
}

Please mark this as Best Answer if you find this solution helpful.

Thank You
Ajay Dubedi

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Deepu,

I trust you are doing very well.

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger CopyFromParentUpdate on Account (after insert, after update) {
    
    Set<id> setOfParentId = new Set<Id>();
    for(Account pt : trigger.new){
        setOfParentId.add(pt.id);
    }
    List<Contact> listChild = new List<Contact>([Select id, AccountId from Contact where AccountId in: setOfParentId]);
    List<Contact> updatedlistChild = new List<Contact>();
        for(Account pt : trigger.new){
            for(Contact ch : listChild){
                ch.Phone = pt.Phone;
                updatedlistChild.add(ch);
            }
        }
    if(updatedlistChild.size()>0){    
        Update updatedlistChild;
    }
}


I hope it helps you.

Kindly 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 and Regards,
Khan Anas
Ajay K DubediAjay K Dubedi
Hi Deepu,
The code is given below:

trigger tri on Account (after update) {
    
   tri_helper.acontPhon(trigger.new);
}

public class tri_helper {
    public static void acontPhon(list<Account> acList)
    {
       List<Contact> cList=new List<Contact>();
       
        for(Contact c:[SELECT Id,phone,account.phone From Contact Where AccountId IN:acList])
        {
            c.phone=c.Account.phone;
            cList.add(c);
            
        }
        if(cList.size()>0)
        update cList;

}
}

Please mark this as Best Answer if you find this solution helpful.

Thank You
Ajay Dubedi
This was selected as the best answer
Raj VakatiRaj Vakati
trigger CopyFromParentUpdate on Account (after insert, after update) {
    
    Set<id> setOfParentId = new Set<Id>();
    for(Account pt : trigger.new){
        setOfParentId.add(pt.id);
    }
    List<Contact> listChild = new List<Contact>([Select id, AccountId from Contact where AccountId in: setOfParentId]);
    List<Contact> updatedlistChild = new List<Contact>();
        for(Account pt : trigger.new){
            for(Contact ch : listChild){
				if(pt.Phone!=null){
                ch.Phone = pt.Phone;
                updatedlistChild.add(ch);
				}
            }
        }
		if(updatedlistChild.size()>0){
    Update updatedlistChild;
		}
}