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
ForceRookieForceRookie 

Update related child record when Opportunity is Closed Won

How to write in trigger when the Opportunity is Closed Won the related Child Record’s Amount field will be equal to Opportunity’s Amount field.
Best Answer chosen by ForceRookie
bhanu_prakashbhanu_prakash
Hi,
Mark as best answer, If it resloves !!​
trigger updateAmount 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, amount from account where Id IN :accIds]){
		for(opportunity opp:trigger.new){
			if(opp.stage=='closed won'){
				a.amount = opp.amount;
				accounts.add(a);
			}
		}
	}
    update accounts;
}

Mark as resloved if it helps :) :)
Thanks, 
Bhanu Prakash
visit ForceLearn.com ​ 

All Answers

NagendraNagendra (Salesforce Developers) 
Hi ForceRookie,

May I suggest you please check below link from the stack exchange community with a similar requirement which has some sample code in it.

Tweak the code as per your requirement which should help. Please let us know if this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra
bhanu_prakashbhanu_prakash
Hi,
Mark as best answer, If it resloves !!​
trigger updateAmount 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, amount from account where Id IN :accIds]){
		for(opportunity opp:trigger.new){
			if(opp.stage=='closed won'){
				a.amount = opp.amount;
				accounts.add(a);
			}
		}
	}
    update accounts;
}

Mark as resloved if it helps :) :)
Thanks, 
Bhanu Prakash
visit ForceLearn.com ​ 
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hii ForceRookie,

Apex Class:-

public class UpdateChildObjectField 
{
 public static void updatemethod(List<opportunity> oplist)
 {
     List<OpportunityLineItem> oplI=[SELECT Name,product2Id from OpportunityLineItem where OpportunityId In:oplist];
    
    
     set<id> setid=new set<id>();
     
     for(OpportunityLineItem li:oplI)
     {
         setid.add(li.product2ID);
     }
     
     List<product2> prolist=[SELECT Amount__c from Product2 where Id In:setid];
     List<Product2> newprolist=new List<Product2>();
     
    for(Opportunity op:oplist)
    {
        if(op.stagename=='Closed won')
        {
            for(Product2 proloop:prolist)
            {
                proloop.Amount__c=op.Amount;
                newprolist.add(proloop);
            }
        }
    }
     update newprolist;
 }
}

Trigger:-
trigger UpdateChildObjectFieldTrg on Opportunity (before update)
{
 UpdateChildObjectField.updatemethod(trigger.new);
}

I hope this code, will help you but there is no field like Amount on the product, so you need to create a custom field on it.

Please mark it as best answer if it helps you.

Thank You,
Ajay Dubedi
ForceRookieForceRookie
Hi. Thanks for your answers. I’ll try those codes later.
And for additional info, my Custom Object (child) looks up to Opportunity.
So should I use setID.add(Opp.Name); ??
ashwini bhattashwini bhatt
Hi Forcerookie,
Once try these below steps
1) From opportunity: Use process builder and update all child records when opportunity is closed won (process builder on opportunity)
2) From child object: use process builder and update child record with parent opportunity amount value if opportunity is closed won
No need of trigger.

Mark as solved if the reply was helpful.

Thankyou,
Ashwini bhatt
ForceRookieForceRookie
Hi bhanu, I need some help because the trigger is working only when I update the opportunity. But when the custom object is created looking up to the opportunity which is already a Closed Won, it doesn’t automatically equals to the Opportunity’s amount (unless I edit the opportunity).