0

I have an invoice payment child object (gii__ARInvoicePayment__c) related to the parent invoice object (gii__OrderInvoice__c).

I created a trigger on the child, that updates the payment date on the invoice when the full payment is posted.

There is a status formula field on the parent that updates to "PAID" when the balance on the invoice is equal to zero. The balance is determined by a currency field called Invoice Total - the roll up sum of the Payment field on the child records (gii__ARInvoicePayment__c). It doesn't get updated to "PAID" until after the gii__ARInvoicePayment__c record is saved so I think that is causing my trigger to not update the payment date field on the parent object. Does anyone have any suggestions on how I can go about this? Thanks!

trigger Update_Payment_Date on gii__ARInvoicePayment__c (after insert, after update) {
    List<gii__OrderInvoice__c>lstInvoiceToUpdate = new List<gii__OrderInvoice__c>();
    Map<Id,gii__OrderInvoice__c>invoiceMP;

    for(gii__ARInvoicePayment__c pmt:trigger.new)
    {
        if (pmt.gii__PaymentDate__c != null && pmt.Invoice_Status__c == 'PAID')
        {
            lstInvoiceToUpdate.add(new gii__OrderInvoice__c(Id = pmt.gii__Invoice__c, Paid_Date__c = pmt.gii__PaymentDate__c, gii__PaymentDate__c = pmt.gii__PaymentDate__c));
        }
    }
    update lstInvoiceToUpdate;
}
3
  • 1
    It'd be more clear what your object hierarchy here is if you would use the names of the objects, I think (i.e. Parent = gii__OrderInvoice__c, Child = gii__ARInvoicePayment__c). It's also not clear what your problem is. What does "it's not working" mean? How is it supposed to work, and how is that different from what you're observing? Are you getting any error messages? I think we'll need this information, and more information about the flow of data/events, to be able to provide any suggestions. You should edit your question to include these details.
    – Derek F
    Commented Jul 9, 2019 at 16:00
  • Thank you for your feedback. I've updated my post.
    – ad_rey
    Commented Jul 9, 2019 at 16:11
  • 1
    I still think that you should expand on the order of events here. I think you've implied that Invoice_Status__c is a formula field, but it'd be good for you to explicitly confirm that. Given that information, answering the following questions should help get you an answer: How does an ARInvoicePayment end up with a zero balance (Does a human manually do it? A workflow rule? Process Builder? Apex? something else?)? Who or what sets the gii__Payment_Date__c? Can there be multiple gii_ARInvoicePayment__c per gii__OrderInvoice__c or is there some business rule that mandates this is a 1:1?
    – Derek F
    Commented Jul 9, 2019 at 16:21

1 Answer 1

2

Based on your description of the data model, this is an order of execution issue.

Your child-object trigger goes off at Step 7 of the save process for the child objects.

Executes all after triggers.

At that point, roll-up summary fields on the parent have not yet been updated, because that change doesn't take place until Step 16:

If the record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the parent record. Parent record goes through save procedure.

In Step 16, we recurse to parent-object update triggers if the roll-up summary field is updated, but it's too late for your child-object triggers to react to the update - they've already run, and already seen data on the parent that's before the update to roll-up summary fields.

Solution: move this logic to a parent-object trigger. It sounds like what you need to do is, when the balance goes to zero, query for payments related to the invoice and populate the payment date fields with the latest payment date (or simply today's date, if that meets your requirements).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .