1

I have custom objects named Order and Order Line Item, just like Opportunity and Opportunity Line Item, they have same fields and relationships but Order Line Item has no need for Pricebook, products can be added directly. Also Order object has Lookup to Opportunity so every Order has a related Opportunity. What I need is when add a product to Order Line Item of the Order, same product must be added to Opportunity Line Item of the related Opportunity.

I have this trigger but i get some errors:

trigger CreateOpportunityProduct on Order_Line_Item__c(after insert, after update){       
if(trigger.isInsert)
{          
List<ID> pricebookID=new List<ID>();       
List<ID> opportunityID=new List<ID>();           
List<OpportunityLineItem> newopportunitylineitemlist=new List< OpportunityLineItem>();        
for(Order_Line_Item__c orderlist: Trigger.new){    
OpportunityLineItem Oppitem=new OpportunityLineItem();
Oppitem.Quantity=orderlist.Quantity__c;
Oppitem.UnitPrice=orderlist.UnitPrice__c;
pricebookID.add(Oppitem.PriceBookEntryId);
opportunityID.add(Oppitem.OpportunityId);
newopportunitylineitemlist.add(Oppitem);
}    
List<Order__c> orderlist= new List<Order__c>([Select Id,Opportunities__c From Order__c Where Opportunities__c IN:opportunityID]);            
List<PricebookEntry> pricebookentrylist = new List<PricebookEntry>([Select Id,Product2Id From PricebookEntry Where Id IN:pricebookID]);         
if(newopportunitylineitemlist.size()>0)
insert(newopportunitylineitemlist);    
}    
}

And the error is:REQUIRED_FIELD_MISSING, Required fields are missing: [Opportunity]: [Opportunity]: Trigger.CreateOpportunityProduct: line 20, column 1

1

As the error states, you need to specify the opportunity id. Presumably, you'll find the opportunity attached to the order.

That probably looks like this:

trigger CreateOpportunityProduct on Order_Line_Item__c(after insert, after update){       
if(trigger.isUpdate)
{          
    map<id, order__c> orders = new map<id, order__c>();
List<ID> pricebookID=new List<ID>();       
List<ID> opportunityID=new List<ID>();           
List<OpportunityLineItem> newopportunitylineitemlist=new List< OpportunityLineItem>();        
    for(order_line_item__c record: trigger.new) {
        orders.put(record.order__c, null);
    }
    orders.putall([select id, opportunity__c from order__c where id in :orders.keyset()]);
for(Order_Line_Item__c orderlist: Trigger.new){    
OpportunityLineItem Oppitem=new OpportunityLineItem();
Oppitem.Quantity=orderlist.Quantity__c;
Oppitem.UnitPrice=orderlist.UnitPrice__c;
oppitem.opportunityid = orders.get(orderlist.order__c).opportunity__c;
pricebookID.add(Oppitem.PriceBookEntryId);
opportunityID.add(Oppitem.OpportunityId);
newopportunitylineitemlist.add(Oppitem);
}    
List<Order__c> orderlist= new List<Order__c>([Select Id,Opportunities__c From Order__c Where Opportunities__c IN:opportunityID]);            
List<PricebookEntry> pricebookentrylist = new List<PricebookEntry>([Select Id,Product2Id From PricebookEntry Where Id IN:pricebookID]);         
if(newopportunitylineitemlist.size()>0)
insert(newopportunitylineitemlist);    
}    
}

Adjust field names as necessary.

  • Same error still stands, we specified OpportunityId, I don't understand what is missing for Opportunity. – user5204 Jan 15 '14 at 1:29
  • It's (trigger.isInsert),sorry it was written wrong, I updated it. – user5204 Jan 15 '14 at 1:37
  • 1
    @Onur You cannot create an opportunity line item without an opportunity. Make sure your order has an opportunity first. – sfdcfox Jan 15 '14 at 2:06
  • Yes every order has a related opportunity. – user5204 Jan 15 '14 at 2:15
0

Give this a try, I've added comments so you can see whats going on and you'll probably need to set the field names to match your data model.

trigger CreateOpportunityProduct on Order_Line_Item__c (after insert, after update) {       

    if (trigger.isInsert) {   

        // Maps to hold related objects
        Map<Id, Id> orderLineItemToOpportunityMap = new Map<Id, Id>();
        Map<Id, Id> productToPricebookEntryMap = new MapId, Id>();

        // First, collect the Order Line Item & Product IDs
        for (Order_Line_Item__c orderlist : Trigger.new) {

            // Add the Order Line Item first of all
            if (orderList.Order__c != null) {

                orderLineItemToOpportunityMap.put(orderlist.Id, null);          
            }

            // Now the Product - I don't know what your Product relationship field is to put it here
            if (orderlist.Product__c != null) {

                productToPricebookEntryMap.put(orderlist.Product__c, null);         
            }           
        }

        // Now populate orderLineItemToOpportunityMap with each Order Line Items related Opportunity IDs
        for (Order_Line_Item__c olic : [    SELECT 
                                                Id,
                                                Order__c,
                                                Order__r.Opportunity__c
                                            FROM
                                                Order_Line_Item__c
                                            WHERE
                                                Id IN :orderLineItemToOpportunityMap.keySet()   ]) {

            if (olic.Order__r.Opportunity__c != null) {

                orderLineItemToOpportunityMap.put(olic.Id, olic.Order__r.Opportunity__c);           
            }       
        }

        // Now populate productToPricebookEntryMap with the Products standard PribebookEntry IDs
        for (PricebookEntry pbe : [ SELECT
                                        Id,
                                        ProductId
                                    FROM
                                        PricebookEntry
                                    WHERE
                                        ProductId IN :productToPricebookEntryMap.keySet()
                                    AND
                                        isStandard = TRUE   ]) {

            productToPricebookEntryMap.put(pbe.ProductId, pbe.Id);          
        }

        // This is the list of OpportunityLineItem records to insert
        List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();

        // Now we have everything we need, time to create the OpportunityLineItem records
        for (Order_Line_Item__c orderlist : Trigger.new) {    

            // Check that there is a PBE ID & Opp. ID present for this record.
            if (orderLineItemToOpportunityMap.get(orderlist.Id) != null && 
                productToPricebookEntryMap.get(orderlist.Product__c) != null) {

                // Related IDs
                Id oppId = orderLineItemToOpportunityMap.get(orderlist.Id);
                Id pbeId = productToPricebookEntryMap.get(orderlist.Product__c);

                // OLI creation
                OpportunityLineItem oli = new OpportunityLineItem(
                    Quantity = orderlist.Quantity__c,
                    UnitPrice = orderlist.UnitPrice__c,
                    OpportunityId = oppId,
                    PricebookEntryId = pbeId
                );

                oliList.add(oli);

            // If Opportunity or PricebookEntry ID is missing for this record, display an error message
            } else {

                if (orderLineItemToOpportunityMap.get(orderlist.Id) == null) {

                    orderlist.addError('Unable to create a related Opportunity Line Item as we could not find this Order Line Items related Opportunity!');             
                }

                if (productToPricebookEntryMap.get(orderlist.Product__c) == null) {

                    orderlist.addError('Unable to create a related Opportunity Line Item as we could not find the standard Pricebook Entry for this Order Line Items Product!');                    
                }           
            }
        }

        // Finally, insert the OpportunityLineItem records
        if (!oliList.isEmpty()) {

            insert oliList;     
        }
    }
}  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy