How i can create inventory custom child object by trigger under opportunity, because child object cannot be created for opportunity line item.

inventory will be created for each line item example-product"A" quantity 2, product "B" quantity 3, after closing the opportunity, inventory will be created -A, -A, -B, -B, -B total 5 with other custome fields product price etc

2 Answers 2


Thats a good question, since it's really impossible to create a Lookup realtionship to OLI.

Next best you could do is to create a Lookup- or Master/Detail-Relationship on Opportunity and add an other custom field (e. g. external Id text(30) ) and store the associated OLI id.

But you won't get many features you usually have for relationships. And you might have to query twice all the time with an WHERE filter to match the association.

You might want to reconsider the second requirement, since for very high quantities you can easily end up with a huge number of custom objects. It will work to a point, but may possibly exhaust your storage limits soon. Expect limit issues for quantities > 2k or 10k or 50k. Introduce an If-statement to prevent huge quantities from polluting your database. If that requirement persists you have to write the logic to maintain the right inventory objects - differently for open and closed opps.

  • I have create master detail with opportunity. But how i can create the record as per quantity using trigger?
    – Sush
    Commented Mar 4, 2015 at 22:51

I have written below logic for your trigger. Please note that I didn't compile the code as I do not have the object structure so you have to make change in the code according to your object and field names. I have tried to write kind of a pseudo code to show you the logic for this requirement. This logic will fail if your trigger is trying to create more than 10k inventory records please consider the upper limits for your requirement scenarios based on data in your org.

trigger OpportunityInverntoryTrigger on Opportunity (after update) {

if( trigger.isAfter && trigger.isUpdate) {
    Set<Id> opportunityIdsToProcess = new Set<Id>();
    Lis<Opportunity_Inventory__c> oppInventoryList = new List<Opportunity_Inventory__c>();
    for(Opportunity oppRecord : trigger.new ) {
        if( oppRecord.StageName = 'Closed Won' && trigger.oldMap.get(oppRecord.Id).StageName <> oppRecord.StageName ) {

    for(Opportunity oppRecord : [ SELECT Id, (SELECT Id , Product2.Name , Product2.UnitPrice, Quantity From OpportunityLineItems ) FROM Opportunity WHERE Id IN :opportunityIdsToProcess]) {

        if(oppRecord.OpportunityLineItems <> null && !oppRecord.OpportunityLineItems.isEmpty()) {

            for(OpportunityLineIte oli : oppRecord.OpportunityLineItems) {
              for(Integer i =0 ; i< oli.quantity ; i++ ) {
                Opportunity_Inventory__c inverntoryRecord = new Opportunity_Inventory__c();
                inverntoryRecord.Opportunity__c = oppRecord.Id;
                inverntoryRecord.Price__c = oli.Product2.UnitPrice;
                inverntoryRecord.Name = oli.Product2.Name;

    if(!oppInventoryList.isEmpty()) {
        insert oppInventoryList;


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .