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
Priya MishraPriya Mishra 

Trigger to Copy the child field value to the parent object custom field.


In Account object we have a field called opportunity status and in opportunity object we have a field called 
Stage so when stage value will change the new value should display in the 
Account object field called opportunity status using trigger.
Best Answer chosen by Priya Mishra
Khan AnasKhan Anas (Salesforce Developers) 
Hi Priya,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger CopyOppToAccPicklist on Opportunity (after insert, after update) {
    
    List<Id> accIds = new List<Id>();
    List<Account> accounts = new List<Account>();
    
    for(Opportunity o : trigger.new){
        accIds.add(o.accountId);
    }
    
    for(Account a : [SELECT Id, Opportunity_Status__c FROM Account WHERE Id IN :accIds]){
        for(Opportunity opp:  trigger.new){
            a.Opportunity_Status__c=opp.StageName;
            accounts.add(a);
        }
    }
    if(accounts.size()>0){
        Update accounts;
    }
}

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

All Answers

abhishek singh 497abhishek singh 497
Hello Priya,
Please find below code I have used Account and Contact you can change object according to yours and fields also.

trigger updateParentField on Contact (after update) {
    Map<ID,Account> contactMap = new Map<ID,Account>();
    Set<Id> accountIdSet = new Set<Id>();
    for(Contact con: trigger.new){
        accountIdSet.add(con.AccountId);
    }
    contactMap = new Map<ID,Account>([SELECT id,Description,(SELECT id,Description FROM Contacts) FROM Account WHERE ID IN:accountIdSet]);
    for(Contact con : trigger.new){
       Account myNewAccount = contactMap.get(con.AccountId);
       myNewAccount.Description = con.Description; 
    }
    update contactMap.values();
}
Please mark it as a best answer if you found it helpful.

Thanks & Regards,
Abhishek Singh.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Priya,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger CopyOppToAccPicklist on Opportunity (after insert, after update) {
    
    List<Id> accIds = new List<Id>();
    List<Account> accounts = new List<Account>();
    
    for(Opportunity o : trigger.new){
        accIds.add(o.accountId);
    }
    
    for(Account a : [SELECT Id, Opportunity_Status__c FROM Account WHERE Id IN :accIds]){
        for(Opportunity opp:  trigger.new){
            a.Opportunity_Status__c=opp.StageName;
            accounts.add(a);
        }
    }
    if(accounts.size()>0){
        Update accounts;
    }
}

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
This was selected as the best answer
Priya MishraPriya Mishra
Thanks Khan Anas it is working fine as expected.