Need help, I'm trying to figure out how can I make a trigger to update Quantity On Hand in the Product Object when an Opportunity is Closed Won.
- OpportunityLineItems -> Quantity (Standard Field) (Number)
- Product2 -> QtyOnHand (Custom Field) (Number)
- ( Product2.QtyOnHand__c – OpportunityLineItems.Quantity ) = Product2.QtyOnHand__c
Opportunity Stage Updated to Closed Won, then run trigger.
Best Answer
I don't think it's wise to rely on the state of a field prior to the start of a transaction as an accurate count. Instead, you should just use a trigger on Opportunity and an aggregate query on the OpportunityLineItem object, which looks up to both Product2 and Opp.
First collect all of your Product2 Ids from the trigger set:
Next, do an aggregate query on the Closed Won opps related to that Product:
EDIT: You edited your post in the middle of my answer to change what Quantity On Hand means. You need a field for how many you have and then how many have been used (the calculation I listed) and a formula to do the subtraction for 'On Hand'