0

I am trying to create a trigger which is after insert and after update whenever i create order the opportunitline item will be added to the orderline item.I have created lookup relationship from order to opportunity.I have done as follwing but it is not creating order line item.

> trigger creatingMenuDetails on Order (after insert,after update)  {
    >     list<OrderItem> MD=new list<OrderItem>();
    >     for(Order op:trigger.new)
    >     {
    >         list<OrderItem> MD1=[SELECT AvailableQuantity,ListPrice,OrderId,OrderItemNumber,Quantity,UnitPrice
    > FROM OrderItem where OrderItemNumber=:op.id ];
    >         list<OpportunityLineItem> pd1=[SELECT Description,Id,ListPrice,Name,OpportunityId,Product2Id,ProductCode,Quantity,TotalPrice,UnitPrice
    > FROM OpportunityLineItem where Name=:op.Opportunity__c];
    >         
    >        if(MD1.size()==0)
    >         {
    >             for(integer i=0;i<pd1.size();i++)
    >             {
    >                 OrderItem om=new OrderItem();
    >                 om.OrderId=op.id;
    >                                                             
    >                 MD.add(om);
    >             }
    >         }
    >     } insert MD; }
    > 
5
  • 1
    Not sure of your structure but I believe you have issues with your where clauses. I doubt OrderItemNumber will ever equal op.id. In addition, your trigger should be on Opportunity and not order right?
    – Eric
    Commented Jul 11, 2015 at 12:44
  • My requirement is some like this ,I have created lookup relationship from order to opportunity .Whenever I create i new order it should pull opportunity line item from opportunity to orderlineitem. Commented Jul 11, 2015 at 12:50
  • you need to rewrite the trigger as you are doing soql inside of for loops. The soql has to be done first with the results saved in maps. Plenty of examples in the SFDC doc or google on bulkification. As an aside, you should edit your post and remove those > to make reading your code easier
    – cropredy
    Commented Jul 11, 2015 at 17:48
  • I will bulkify it later but m not getting results Commented Jul 11, 2015 at 18:06
  • 1
    Please never say "I'll will bulkify it later" alway, always, always bulkify first, that should be a mentality. Commented Jul 11, 2015 at 20:15

1 Answer 1

1

Hopefully this helps out, just did a quick refactor with no testing so you may still need to do a little brush up work.

trigger creatingMenuDetails on Order (after insert, after update)
{
    // Get all related opportunities from orders
    Set<Id> opportunityIds = new Set<Id>();
    List<Order> orderList = new List<Order>();
    for(Order o : Trigger.new)
    {
        if(o.Opportunity__c != NULL)
        {
            opportunityIds.add(o.Opportunity__c);
            orderList.add(o);
        }
    }

    // Query for all opportunities with their related opportunity line items
    Map<Id, Opportunity> oppsWithLineItems = new Map<Id, Opportunity>([SELECT Id, (SELECT Description,Id,ListPrice,Name,OpportunityId,Product2Id,ProductCode,Quantity,TotalPrice,UnitPrice FROM OpportunityLineItem) WHERE Id IN :opportunityIds]);

    if(opportunityIds.size() > 0)
    {
        // Loop through orders
        List<OrderItem> orderItemsForInsert = new List<OrderItem>();
        for(Order o : ordersList)
        {
            // For each order get the related opportunity and line items, loop through the line items and add a new order line item to the order line item list for each matching opportunity line item
            Opportunity oppWithLineItem = oppsWithLineItems.get(o.Opportunity__c);
            for(OpportunityLineItem oli : oppWithLineItem.OpportunityLineItems)
            {
                orderItemsForInsert.add(new OrderItem(AvailableQuantity=Quantity,OrderId=o.Id,etc,etc,etc));
            }
        }
        // If we have order line items, insert data
        if(orderItemsForInsert.size() > 0)
        {
            insert orderItemsForInsert;
        }
    }
}
6
  • Thanks ..Ihave brushed up code it's working when pricebook is set for order ..if i create new order it's showing this error:"" Commented Jul 11, 2015 at 20:44
  • FIELD_INTEGRITY_EXCEPTION, Price Book Not Set on Order: []: Trigger.Details: line 34, column 1 Commented Jul 11, 2015 at 20:44
  • @SalesforceLearner - Order.pricebook2id is a required field - see developer.salesforce.com/docs/atlas.en-us.object_reference.meta/… - BTW - give @techbusnessman credit for bulkifying your trigger
    – cropredy
    Commented Jul 12, 2015 at 0:45
  • If u see above code m inserting order item how to set price book for order before that... Commented Jul 12, 2015 at 3:58
  • I have to get same price book as opportunity line item... I understand it is required but where should I add in above code Commented Jul 12, 2015 at 3:59

You must log in to answer this question.

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