[SalesForce] Trigger On Order to get Opportunity Line Item from opportunity to add in orderlineitem

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; }
    > 

Best Answer

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;
        }
    }
}