0

I am trying to create Order Delivery Method and Order Delivery group for each Order Item when Order Item record is created. After that I need to update Order item with Order Delivery Group. But when I create an Order Item record am getting this error

AfterInsert caused by: System.FinalException: Record is read-only ()

Below is my code:

trigger CreateOrderSummary on OrderItem (after insert,after update)
{
    List<OrderDeliveryMethod> odmList=new List<OrderDeliveryMethod>();
    List<OrderDeliveryGroup> odgList=new List<OrderDeliveryGroup>();
    list<OrderItem> otList=new List<OrderItem>();
    List<Id> otId=new List<Id>();
    for(OrderItem ot: trigger.new){
        if(ot.SBQQ__Status__c=='Draft'){
            OrderDeliveryMethod odm=new OrderDeliveryMethod();
            odm.Name=ot.OrderItemNumber;
            odm.IsActive=True;
            odm.ProductId=ot.Product2Id;
            odmList.add(odm);
      
            OrderDeliveryGroup odg=new OrderDeliveryGroup();
            odg.DeliverToName='Shipping Address';
            odg.OrderId=ot.OrderId;
            odg.Product__c=ot.Product2Id;
            odg.Order_Product__c=ot.Id;
            odgList.add(odg); 
            otList.add(ot);
            otId.add(ot.Id);
        }
    }
    insert odmList;
    insert odglist;
    for(OrderDeliveryMethod od:odmList) {
        for(OrderDeliveryGroup og: odgList){
            if(od.ProductId==og.Product__c){
               og.OrderDeliveryMethodId=od.Id; 
            }
        }
    }
    update odgList;
    for(OrderDeliveryGroup og: odgList){
        for(OrderItem ot:otList) {
            if(ot.Id==og.Order_Product__c){
               ot.OrderDeliveryGroupId=og.Id; 
            }
        }
    }
    update otList; 
}

It works fine if I remove below code. But I want to update Order Item with OrderDeliveryGroup after created.

for(OrderDeliveryGroup og: odgList){
    for(OrderItem ot:otList) {
        if(ot.Id==og.Order_Product__c){
            ot.OrderDeliveryGroupId=og.Id; 
        }
    }
}
update otList;

I tried with trigger.isBefore, when I used that I am not getting an error but OrderDeliveryGroup is not updated on Order Item.

Kindly let me know if missed something

2

The records that cause a trigger to run are read-only in the after context. In order to update those records in the after context, you'll need to create a copy of them. See below:

    for(OrderItem ot: trigger.new){
        if(ot.SBQQ__Status__c=='Draft'){
            OrderDeliveryMethod odm=new OrderDeliveryMethod();
            odm.Name=ot.OrderItemNumber;
            odm.IsActive=True;
            odm.ProductId=ot.Product2Id;
            odmList.add(odm);
      
            OrderDeliveryGroup odg=new OrderDeliveryGroup();
            odg.DeliverToName='Shipping Address';
            odg.OrderId=ot.OrderId;
            odg.Product__c=ot.Product2Id;
            odg.Order_Product__c=ot.Id;
            odgList.add(odg); 
            // add a copy of the OrderItem instead of the original
            otList.add(new OrderItem(Id = ot.Id));
            otId.add(ot.Id);
        }
    }

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.