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.