0

If any Order Products have Fulfillment Process = "Manual", the Fulfillment flag on the Order is set to "True".

I have wrote a trigger for this but it is working when I create a order product where manual is default value of the pick list. Then I create another order product but when I go for update it does not reflect to update checkbox. Could anybody help me to find where I am getting wrong please.

My class logic:

public static void OrderfullfillmentProcess(list<OrderItem>conlist){
    set<id>ids= new set<id>();
    for(OrderItem odrIT:conlist){
        ids.add(odrIT.OrderId);
    }

    list<order>ords=[select id,name, Fulfillment__c, (select id, Fulfillment_Process__c from orderitems) from order where id in:ids];

    for(order o:ords){
        for(orderItem orderIT:o.OrderItems){
            if(orderIT.Fulfillment_Process__c=='Manual'){
                o.Fulfillment__c=true;
            }
            else{
                 o.Fulfillment__c=false;
                }
            }
        }

        update ords;
    }   
} 

My trigger:

if(trigger.isInsert || trigger.isupdate){
     OrderItem_Trigger_Handler.OrderfullfillmentProcess(trigger.new);

     }
  • What did you update order or orderItem? And also you should optimise your trigger code for better trigger event handling. – Salesforcesmarty Mar 6 '17 at 2:44
  • @Salesforcesmarty I am trying update a checkbox on order when order product pick list values is manual and order checkbox should be updated if I change any of the order product pick list values. – maruf Mar 6 '17 at 3:43
  • What happens when there are two order Item with different picklist values under the same order. Will that checkbox be checked on order? Also did you try Eric's code? – Salesforcesmarty Mar 6 '17 at 4:15
  • when there are two different pick list values,example values for order item manual and other one is automatic then checkbox is checked. – maruf Mar 6 '17 at 4:38
0

I am assuming you would be able to resolve normal errors and replace relevant Order fields in the code.

Here is the updated code :

public static void OrderfullfillmentProcess(list<OrderItem>conlist){
    Order[] orderTBU = New Order[]{};
    Set<Id> orderSet = new Set<Id>();
    Map<Id,Order> uniqueOrders = new Map<Id,Order>();
    Order tmp;
    for(OrderItem oi : conlist){
        if(!uniqueOrders.containsKey(oi.OrderId))
            tmp = New Order(Id = oi.OrderId); //Or whatever the Id field is of the Order relationship
        else
            tmp = uniqueOrders.get(oi.OrderId);

        if(!orderSet.contains(tmp.Id)){
            if(oi.Fulfillment_Process__c=='Manual'){
                tmp.Fulfillment__c=true;
                orderSet.add(tmp.Id);
            }
            else{
                 tmp.Fulfillment__c=false;
            }
        }

        if(!uniqueOrders.containsKey(oi.OrderId))
            uniqueOrders.put(tmp.Id,tmp);
    }

    update uniqueOrders.values();

}

Updated Trigger code, should be something like this

Trigger triggerName on OrderItem (After Insert, After Update){
    if(trigger.isInsert || trigger.isupdate){
        OrderItem_Trigger_Handler.OrderfullfillmentProcess(trigger.new);

    }
}
0

If this is a before trigger then the problem is that you are querying for records that have not been updated yet.

The entire class could be done as follows:

public static void OrderfullfillmentProcess(list<OrderItem>conlist){
    Order[] orderTBU = New Order[]{};
    for(OrderItem oi : conlist){
        Order tmp = New Order(Id = oi.Order__c); //Or whatever the Id field is of the Order relationship

        if(oi.Fulfillment_Process__c=='Manual'){
            tmp.Fulfillment__c=true;
        }
        else{
             tmp.Fulfillment__c=false;
            }
        }
        orderTBU.add(tmp);
    }

    update orderTBU;

}

This will essentially get the value of the OrderItem field from the trigger records and update the parent order as needed.

Note This does not handle the case where multiple OrderItems could have differing values. You will need to decide if that is an issue and figure out how to resolve the contention.

  • @maruf - Are you still stuck? You did not mark anything as accepted and your comment is unclear – Eric Mar 6 '17 at 15:14
  • It is working when you create order item, but when you update, it is not working. Example: two order item. When you create 1st one with default pick list values manual, checkbox checked. But if you change the status to automated (for example) it is not working . Now when you create 2nd order item, it is working again but for update it is not working . Here Is my updated code : – maruf Mar 9 '17 at 17:28
  • : How could I add my code, It says too long ? – maruf Mar 9 '17 at 17:41

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.