We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

How to access MRVS variables (on Change Script)- Record Producer

supriya pratapa
Tera Guru

I have a variable called total_cost in my MRVs, and I want to calculate the sum of total_cost across all rows. I’ve created another variable called total cost of all products, and the total of total_cost from each row should be reflected in this variable.

 

tried below from community:
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) return;

    var mrvsName = g_form.getValue("IO:744dd78d775321105c24f664be5a9939");
    var outputVar = 'total_cost_of_the_products';
    var total = 0;
g_form.addInfoMessage(mrvsName);
    var mrvsJSON = g_form.getValue(mrvsName);
    if (!mrvsJSON) {
        g_form.setValue(outputVar, '$0.00');
        return;
    }

    try {
        var rows = JSON.parse(mrvsJSON);
        rows.forEach(function(row) {
            var rawValue = row.total_cost || '0';
            var cleanValue = rawValue.replace(/[^0-9.-]/g, '');
            var numericValue = parseFloat(cleanValue) || 0;
            total += numericValue;
        });

        var formatted = '$' + total.toFixed(2);
        g_form.setValue(outputVar, formatted);
    } catch (e) {
        g_form.setValue(outputVar, '$0.00');
    }
}
 
 
supriyapratapa_1-1761488845110.png

 

 

 
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@supriya pratapa 

see this link and enhance

Populate field outside MRVS with a total sum of fields inside MRVS 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

1 REPLY 1

Ankur Bawiskar
Tera Patron

@supriya pratapa 

see this link and enhance

Populate field outside MRVS with a total sum of fields inside MRVS 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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