I need to update the Order record when it's status changes to Activated. I want to set the custom field, invoice_Due_date__c based on a related record's value. The related record is child record in the object Payment_Terms__c and there will only ever be 1 record related and a field number_of_days__c populated.

Ultimately when the order is activated I want to set the field, invoice_due_date__c to the activateddate + number_of_days__c from the child record.

I am really stuck with this, I couldn't see how to manage it in the process so i assume the best way is to run an beforeupdate trigger on the order object. I have some experience with basic triggers and came up with the following but I know I am going wrong somewhere. Any advice would be appreciated:


The following sits in my OrderActions class which is called by a trigger handler/trigger via the beforeUpdate context just now. It brings up a cannot de-reference null object error. I've tried a few varitaions but also understand a for inside a for is poor design.

public static void OrderAddInvoiceDueDate(List<Order> Order){
        Set<Id> idSet= new Set<Id>(); //set of Id's
        //popupate the set with the Order ID's
        for (Order o : (List<Order>) Order){            
        List<Payment_Terms__c> pt = new List<Payment_Terms__c>([SELECT id, number_of_days__c, order__c FROM Payment_Terms__c WHERE order__c IN :idSet]);
        for (order orlist: order){
            if (orlist.status == 'Activated'){
                for (Payment_Terms__c ptexists: pt){
                    if (ptexists.order__c == orlist.id){
                        Date newDate = date.newInstance(orlist.activatedDate.year(), orlist.activatedDate.month(), orlist.activatedDate.day());
                        orlist.Invoice_Due_Date__c = newDate.addDays(ptexists.number_of_days__c.intValue());
  • Why don't you instead have a field say number_of_days_on_payment__c on Order object which always is the current value of number_of_days__c from Payment_Terms__c. And whenever an Order is updated, just use this value from Order instead of going to find out from the child.
    – Jayant Das
    Jan 30 '19 at 15:05

If I were to approach this scenario, I would have done it the following way (considering there's always a 1:1 relationship between your objects).

  1. Create a custom field say number_of_days_on_payment_terms__c field on Order. If you can have the relationship between Order and Payment_Terms__c as a Master-Detail relationship, you can just create number_of_days_on_payment_terms__c as roll up summary field. In that case, you don't even need step 2.
  2. If only you cannot have a Master-Detail relationship between your object, then this step is applicable. Every time the field number_of_days__c changes on Payment_Terms__c record, just copy it over on Order. This can be achieved either through trigger or workflow rule. This ensures that the field on Order is always updated with the value from Payment_Terms__c.

  3. Now, whenever the Order is set to Activated, write the trigger on Order and just use the value on number_of_days_on_payment_terms__c on Order to calculate the invoice_Due_date__c field.

With this approach you eliminate the complexity of going into child records and finding out the values, etc. and that you get the value as required right on the record you are operating on.

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.