Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Based on mrvs variable value set the item value

Venky Kshatriy2
Tera Contributor

Hi Team,

I have a question regarding the MRVS. variable (A). I need to ensure that when A is not empty, a specific value is set in the  variable (B). The A variable is stored in MRVS, while B is at the item level. For this requirement, I implemented a client script that triggers on the change of A to set the value of B.

Here's the script I've written:

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

if (newValue != '') {
g_service_catalog.parent.setValue('B', '1');
}
}
```

However, the value is not being set in variable B as expected. If anyone has experience with this type of requirement, could you please assist me?

Thanks in advance!

2 REPLIES 2

Sohail Khilji
Kilo Patron

Hi @Venky Kshatriy2 ,

 

See if this helps..

 

https://www.servicenow.com/community/developer-articles/solved-issue-with-mrvs-multi-row-variable-se...


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Runjay Patel
Giga Sage

Hi @Venky Kshatriy2 ,

 

Use below script to check if A is empty or not, based on true false set the value.

var allRowsHaveValues = false;
var MRVS_rows = g_form.getValue('product_parameters'); // replace with MRVS variable name
    if (MRVS_rows) {
        try {
            var rows = JSON.parse(MRVS_rows); 
            for (var i = 0; i < rows.length; i++) {
		 if (rows[i]['material_id'] == '') { // replace with variable name pf MRVS
                    allRowsHaveValues = true; // Found a row where A is empty
                    break;
                }
            }
        } catch (e) {
            alert('Error parsing MRVS rows: ', e);
        }
	}
if(allRowsHaveValues){
g_form.setValue('variable_name', 'desired_value');
}

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------