Based On value changes Grand total has to change

suresh40
Tera Contributor

Hi All,

I have created a custom form where the grand total should be calculated based on the values entered in the fields. I have implemented a Business Rule that updates the grand total after saving the form, and it is working fine. However, the issue is that if the user changes the value of any field (e.g., Freight), the grand total should update immediately before saving the form.

How can I achieve this?

business rule script :

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

    var capFreight = parseFloat(current.u_freight_cap) || 0;
    var capStartup = parseFloat(current.u_installation_startup_cap) || 0;
    var capContingency = parseFloat(current.contingency_cap) || 0;
    var capSubTotal = parseFloat(current.u_sub_cap) || 0;
    var capTaxTotal = parseFloat(current.u_tax_total_cap) || 0;

    var totalAll = capSubTotal + capTaxTotal +capFreight + capStartup;
    var contingencyAmount = (totalAll * capContingency) / 100;
    current.u_gtotal_cap = totalAll + contingencyAmount;

    current.u_capital = current.u_gtotal_cap;
  
})(current, previous);



4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@suresh40 

you will require 5 onChange 1 each on these fields and set the total. But better to use 1 before update business rule

u_freight_cap, u_installation_startup_cap, contingency_cap, u_sub_cap, u_tax_total_cap

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@suresh40 

something like this on 1 field and then you will have to repeat for other 4 field

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    // Retrieve the values from the form fields
    var capFreight = parseFloat(g_form.getValue('u_freight_cap')) || 0;
    var capStartup = parseFloat(g_form.getValue('u_installation_startup_cap')) || 0;
    var capContingency = parseFloat(g_form.getValue('contingency_cap')) || 0;
    var capSubTotal = parseFloat(g_form.getValue('u_sub_cap')) || 0;
    var capTaxTotal = parseFloat(g_form.getValue('u_tax_total_cap')) || 0;

    // Perform the calculation
    var totalAll = capSubTotal + capTaxTotal + capFreight + capStartup;
    var contingencyAmount = (totalAll * capContingency) / 100;
    var grandTotal = totalAll + contingencyAmount;

    // Update the form fields with the calculated values
    g_form.setValue('u_gtotal_cap', grandTotal);
    g_form.setValue('u_capital', grandTotal);
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,

I have tired this script but its not working

Thanks,
Suresh

@suresh40 

what debugging did you perform?

those fields are on form or not?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader