You need to sign in to do that
Don't have an account?
ForceRookie
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.
Mark as best answer, If it resloves !!
Mark as resloved if it helps :) :)
Thanks,
Bhanu Prakash
visit ForceLearn.com
All Answers
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.
- https://salesforce.stackexchange.com/questions/138516/copy-opportunity-amount-field-to-custom-object
Please let us know if this helps.Kindly mark this as solved if the reply was helpful.
Thanks,
Nagendra
Mark as best answer, If it resloves !!
Mark as resloved if it helps :) :)
Thanks,
Bhanu Prakash
visit ForceLearn.com
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
And for additional info, my Custom Object (child) looks up to Opportunity.
So should I use setID.add(Opp.Name); ??
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