MRVS-MULTI ROW VARIABLE SET

servicenow14710
Tera Expert

Hello developers,

how to get row count of mrvs or the ask is i need to show another field on catalog form if there is atleast one row added to MRVS. Any solution is appreciated.Thanks!

3 REPLIES 3

Peter Bodelier
Giga Sage

Hi @servicenow14710 

 

You should be able to something like this:

var mrvsRows = g_form.getValue("mrvs_name");
//The above will return a string in the form of array and objects. Now you will have to parse it

var oMrvs = JSON.parse(mrvsRows);
var sum = 0;
for(var i in oMrvs) {
	sum++;
}
if (sum != 0){
g_form.setVisible("field_name", true);
}
else
{
g_form.setVisible("field_name", false);
}

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Hello @Peter Bodelier : Thanks for your quick response tried with the above code in Client script on change of variable in variable set. Am I going correct as i couldnt get the appropriate solution.

Hi @servicenow14710 

 

In your use case it seems to be a multistep process.

I'll try to explain:

 

First create a Yes/No variable in your catalog item, name 'mrvsupdated. This can be a hidden variable.

 

Create an onLoad Client script in your Catalog Item:

function onLoad() {
   //We need to make the g_form object for the MRVS available for later
	    this.cat_g_form = g_form;
   }

Create an onChange Client script which triggers on the 'mrvsupdated' variable.
Change the <<fieldName>> and <<mrvsName>> to the names you need.

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

    g_form.setValue('mrvsupdated', 'no');

    //Need to set a timeout because MRVS is not readable directly.
    setTimeout(function() {
        var mrvsRows = g_form.getValue("<<mrvsName>>");
        var oMrvs = JSON.parse(mrvsRows);

        var sum = 0;
        for (var i in oMrvs) {
            sum++;
        }
        if (sum != 0) {
            g_form.setDisplay("<<fieldName>>", true);
        } else {
            g_form.setDisplay("<<fieldName>>", false);
        }
    }, 500);

}

 

And lastly create an onSubmit Client script in the MRVS.

function onSubmit() {

        //Service Portal logic
        if (this) {
            this.cat_g_form.setValue('mrvsupdated', 'yes');
        } else {
            //Native UI logic
            parent.g_form.setValue('mrvsupdated', 'yes');
        }
}


 The only thing this does not do, is to hide the field again, because I haven't found a way to set the mrvsupdated field to yes, when a row is deleted.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.