Issue with auto displaying of total in the field

TarunChowdary
Tera Contributor

I’m facing an issue with auto displaying the subtotal field in the procurement catalog item,

 

I’m taking the items, quantity and estimated total in multirow variable set and I want to display the cost of all items in the subtotal field in the single row variable set.

In the first attachment the subtotal field is not changing when there is a change in multirow variable set.

But when I put some value in the subtotal field it is automatically overridden, and the actual value pops up

This is the on-change script I’m using on the subtotal field.

 

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

    var totalCost = 0;
    var mrvsData = g_form.getValue('it_item_information');
    if (mrvsData) {
        var itemsArray = JSON.parse(mrvsData);

        itemsArray.forEach(function(row) {
            var extendedCost = row.it_estimated_extension || '0'; // Handle null or empty extended_cost
           
            // Remove any currency symbols, commas, and spaces from extendedCost and convert it to float
            var cleanCost = parseFloat(extendedCost.replace(/[^0-9.-]+/g,"")) || 0;
           
            // Add to the  totalCost
            totalCost += cleanCost;
        });
    }

    // Format the totalCost as USD currency
    var formatter = new Intl.NumberFormat('en-US', {
        style: 'currency',
        currency: 'USD',
    });

    // Convert the total to USD format
    var totalCostFormatted = formatter.format(totalCost);

    // Set the formatted total cost in the sub_total field
    g_form.setValue('it_eup_subtotal', totalCostFormatted);
}
 

Please let me know if anyone has ever faced this kind of issue or if there is any solution to solve this issue.

 

Thanks,

Tarun

 

1 ACCEPTED SOLUTION

Hi @TarunChowdary ,

 

This is how I was able to achieve this, it is kinda workaround but should work fine in your case:

 

This is my sample form:

JohnnySnow_0-1742438433422.png

1. I have added a checkbox to calculate the total, since we dont have an event to determine the addition or deletion of records in the mrvs.

2. I run a client script when the calculate total button is true and make the mrvs readonly. Making it read only is important coz if it is not read only, users can add new records/delete records and we wont get the event(refer pt 1).

3. If they have to enter new record, then they have to uncheck the calculate total checkbox. 

JohnnySnow_1-1742438453745.png

 

Client script on Catalog item:

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


    var calTotal = g_form.getValue('calculate_total'); //calculate total is the checkbox
    if (calTotal == 'true') {

        var multiRowVariableSet = JSON.parse(g_form.getValue('item_list')); //mymrvs name

        var totalCost = 0;
        for (var i = 0; i < multiRowVariableSet.length; i++) {
            totalCost += parseInt(multiRowVariableSet[i].item_value);  
        }
        g_form.setValue("total", totalCost);
    } else if (calTotal == 'false') {
        g_form.setValue("total", "");
    }

and a UI policy to make the MRVS Read only

 

JohnnySnow_2-1742438819749.png

 

 

Please mark this helpful/correct if it worked out fine.

 

 

Thanks
Johnny

Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

It's tricky activating a client script when a MRVS row is added, edited, or deleted as there is not inherently something watching for those activities.  An onSubmit script that applies to the MRVS can get the contents of the MRVS value on the parent form, but the timing of that results in the contents prior to the row that triggered the script, so that's no good.  

 

You can try one of these approaches - some may be dependent on using Service Portal / ESC or the native UI:

https://www.servicenow.com/community/itsm-forum/populate-field-outside-mrvs-with-a-total-sum-of-fiel... 

https://www.servicenow.com/community/developer-forum/actions-after-a-mrvs-update/td-p/3031014 

https://www.servicenow.com/community/developer-forum/mrvs-detect-when-a-row-is-removed-or-deleted/m-... 

Hi Brad,

Thanks for the reply,

 

in this case there are some fields depending on the estimated subtotal

 

TarunChowdary_0-1742417949598.png

I can't really use a OnSubmit script, it will really help if it's changing when a value is being changed in the MVRS or if there is any other way around it.

 

Thanks

Hi @TarunChowdary ,

 

This is how I was able to achieve this, it is kinda workaround but should work fine in your case:

 

This is my sample form:

JohnnySnow_0-1742438433422.png

1. I have added a checkbox to calculate the total, since we dont have an event to determine the addition or deletion of records in the mrvs.

2. I run a client script when the calculate total button is true and make the mrvs readonly. Making it read only is important coz if it is not read only, users can add new records/delete records and we wont get the event(refer pt 1).

3. If they have to enter new record, then they have to uncheck the calculate total checkbox. 

JohnnySnow_1-1742438453745.png

 

Client script on Catalog item:

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


    var calTotal = g_form.getValue('calculate_total'); //calculate total is the checkbox
    if (calTotal == 'true') {

        var multiRowVariableSet = JSON.parse(g_form.getValue('item_list')); //mymrvs name

        var totalCost = 0;
        for (var i = 0; i < multiRowVariableSet.length; i++) {
            totalCost += parseInt(multiRowVariableSet[i].item_value);  
        }
        g_form.setValue("total", totalCost);
    } else if (calTotal == 'false') {
        g_form.setValue("total", "");
    }

and a UI policy to make the MRVS Read only

 

JohnnySnow_2-1742438819749.png

 

 

Please mark this helpful/correct if it worked out fine.

 

 

Thanks
Johnny

Please mark this response as correct or helpful if it assisted you with your question.

Hi JonnySnow,

 

This is Great it totally works,

 

Thank you so much for the response