Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Clear variable values on realtime

Kumar887
Tera Contributor

Hi everyone, I am working on a ServiceNow catalog item where I have a multi row variable set named a and inside it a user reference variable u, and I am using a catalog client script onChange to populate a catalog-level variable uvar which works correctly when I add or update a user, but when I remove a row using the delete icon the onChange script does not trigger and the value in uvar is not cleared, I have tried handling empty values and using clearValue in the same script but it does not execute on row removal, so I want to know what is the recommended or best practice way to detect MRVS row deletion and clear the parent variable immediately using catalog client scripts without using service portal widgets or polling.

script:

function onChange(control, oldValue, newValue, isLoading) {

if (isLoading || !newValue) return;

alert("MRVS field changed: " + newValue);

// From MRVS → Parent form variable
if (typeof parent !== 'undefined' && parent.g_form) {

parent.g_form.setValue('uvar', newValue);

alert("uvar populated successfully: " + newValue);

} else {

// fallback (rare case)
g_form.setValue('uvar', newValue);
}
}

Kumar887_0-1776933746549.png

thanks

2 REPLIES 2

vaishali231
Tera Guru

Hey @Kumar887 

 

This behavior is expected. In ServiceNow, onChange of a variable inside an MRVS does not trigger when a row is deleted. Therefore, your current approach cannot handle row removal.

The recommended approach is to move the client script to the MRVS variable itself and process its JSON value.

Script :

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) return;
    var users = [];
    if (newValue) {
        try {
            var mrvsData = JSON.parse(newValue);
            mrvsData.forEach(function(row) {
                if (row.u) {
                    users.push(row.u);
                }
            });
        } catch (e) {
            console.log('Error parsing MRVS data', e);
        }
    }
    var finalValue = users.join(',');
    // Set or clear parent variable
    if (typeof parent !== 'undefined' && parent.g_form) {
        parent.g_form.setValue('uvar', finalValue);
    } else {
        g_form.setValue('uvar', finalValue);
    }
}

************************************************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

Regards

Vaishali Singh



Ankur Bawiskar
Tera Patron

@Kumar887 

there is a workaround to detect is MRVS row is deleted/added

check this and enhance your code to handle the clear of outside variable

you need to create a variable of type label and include widget in it which observes the MRVS

How to calculate the total of variables into another variable from a multi-row variable set? 

💡 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