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
P 186P 186 

Apex Trigger to Update Contact Detail field field Based on account information filed

I am trying to create a trigger that will update a Account field(Type) and  update in Custom filed in Conatct also.I have created one custom filed(Type) in Contact Detail.
Best Answer chosen by P 186
Khan AnasKhan Anas (Salesforce Developers) 
I have given a general example. Replace that field according to your requirement. Try below code:
 
trigger CopyFromParent on Account (after insert, after update) {
    
    Set<Id> setOfParentId = new Set<Id>();
    for(Account p : trigger.new){
        setOfParentId.add(p.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.Type__c = pt.Type;
                updatedlistChild.add(ch);
            }
        }
    if(updatedlistChild.size()>0){
    	Update updatedlistChild;
    }
}

Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Pankaj,

Greetings to you!

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 CopyFromParent on Account (after insert, after update) {
    
    Set<Id> setOfParentId = new Set<Id>();
    for(Account p : trigger.new){
        setOfParentId.add(p.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.Contact_Field__c = pt.Account_Field__c;
                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. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
P 186P 186
Thanks Anas  for quick reply but can u tell me why you have taken Account_Field__c this field in Account. I am not creating any new field in Account. Type filed is alreday existed in Account Information section.I am creating new custom field in only Contact that is also a Type in Contact detail section.
Khan AnasKhan Anas (Salesforce Developers) 
I have given a general example. Replace that field according to your requirement. Try below code:
 
trigger CopyFromParent on Account (after insert, after update) {
    
    Set<Id> setOfParentId = new Set<Id>();
    for(Account p : trigger.new){
        setOfParentId.add(p.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.Type__c = pt.Type;
                updatedlistChild.add(ch);
            }
        }
    if(updatedlistChild.size()>0){
    	Update updatedlistChild;
    }
}

Regards,
Khan Anas
This was selected as the best answer
P 186P 186
Thanks Anas. now it is working fine as per my requirments.