Update Related records when the current record is updated.

Akshay Kanerkar
Tera Contributor

Hi Folks,
I have an Invoice table and Invoice line table, the fields on the invoice line table are (Payor, Line amount, Invoice) and the fields on the Invoice table are (Payor, Total amount), there are multiple invoice lines associated with the single invoice, the common field between both the table is the payor(which is referenced to payor table), so when I create an invoice line based on the payor selected, the invoice will be automatically created and linked to the Invoice field on the Invoice line table which is referenced to the Invoice, so I want to populate the Total amount field on the Invoice table with the Line amount field value on the Invoice lines associated with the Invoice record. eg. I will create the 2 invoice lines A & B with the line amounts of 10 and 20 respectively, so based on the payor selected the invoice gets created(Assume it INV001) and these two invoice lines(A & B) will be linked to the single Invoice(INV001), so the Total Amount field on the invoice(INV001) should be set as 30(Line amount on A +Line amount on B), if I change the payor for Invoice line A, a new invoice will be created as (INV002), the Total amount of invoice(INV001) should show as 20(Invoice line B) because this is the only one Invoice line it has. Another invoice(INV002) having field total amount should show as 10(Line amount on B). both previous invoice total amount and current invoice total amount should be updated as total from related invoice lines.

 

6 REPLIES 6

Hi,

Can you update Business Rule:

(function executeRule(current, previous /*null when async*/) {

    // Check if the payor field has changed
    if (current.payor.changes()) {
        var invoiceGR = new GlideRecord('your_invoice_table_name');
        if (invoiceGR.get('payor', current.payor)) {
            var invoiceID = invoiceGR.getUniqueValue();
            YourScriptIncludeName.updateInvoiceTotal(invoiceID.toString());
        }
    }

})(current, previous);

 
Script Include:

// Function to update Total Amount on associated Invoice
updateInvoiceTotal: function(invoiceID) {
    var invoiceGR = new GlideRecord('your_invoice_table_name');
    if (invoiceGR.get(invoiceID)) {
        var totalAmount = 0;

        // Query Invoice Lines associated with the Invoice
        var lineGR = new GlideRecord('your_invoice_line_table_name');
        lineGR.addQuery('invoice', invoiceID);
        lineGR.query();

        // Calculate Total Amount from Invoice Lines
        while (lineGR.next()) {
            totalAmount += lineGR.getValue('line_amount');
        }

        // Update Total Amount on Invoice
        invoiceGR.setValue('total_amount', totalAmount);
        invoiceGR.update();
    }
},

@Chaitanya Redd1Since it is After BR, the script include updating the record which is there on the form before the payor changes, I need to update records once the payor is changed so the Invoice, and this invoice should get updated.
Suppose The invoice line has INV001 as the invoice, if I change the payor the invoice will also change to INV002, so the Total amount on INV002 should get populated with its associated Invoice Line amount. The script include you provided is updating INV001 rather than INV002. (We need to pass the sys_id of INV002 which is after the invoice line is updated )