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.

How to get rowCount of mrvs and set the value of a field in Catalog item variable.

jitusingh
Tera Contributor

Hi,

 

I have MRVS if I add a row , it should take few MRVS variable values and set the values in the single line text variable on the catalog item and If i remove the row it will also remove the text form the catalog item variable.

 

Note:- row_size =1; //the row size is set to be not more than 1

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@jitusingh 

you can use below link and detect when new row was added/removed and based on that update the other variable

MRVS detect when a row is removed or deleted 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

How will I get to know the row is added or deleted.

@jitusingh 

that's what is shared in the above link which I mentioned.

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

Amit Gujarathi
Giga Sage
Giga Sage

HI @jitusingh ,
I trust you are doing fine.

To achieve the functionality you described, you can use client-side scripting in the catalog item's variable set. Here's an example of how you can accomplish this:

  1. Create a multi-row variable set (MRVS) with the desired variables.
  2. Add a "OnLoad" client script to the catalog item form to initialize the behavior.
  3. Add a "OnChange" client script to the MRVS variable to update the single line text variable whenever a row is added or removed.
function onLoad() {
    // Disable MRVS if row size is more than 1
    var mrvsVariable = g_form.getVariable('<variable_name>'); // Replace <variable_name> with the actual MRVS variable name
    if (mrvsVariable.getRowCount() > 1) {
        mrvsVariable.setDisabled(true);
    }
}

OnChange script (for the MRVS variable):

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    
    var textVariable = g_form.getControl('<text_variable_name>'); // Replace <text_variable_name> with the actual single line text variable name
    
    if (control.getRowCount() > 0) {
        var firstRow = control.getRow(0);
        var value1 = firstRow.<variable_name1>; // Replace <variable_name1> with the actual MRVS variable name
        var value2 = firstRow.<variable_name2>; // Replace <variable_name2> with another MRVS variable name if needed
        
        // Set the values in the single line text variable
        textVariable.value = value1 + ' ' + value2; // Modify this line based on your desired logic
    } else {
        // Remove the text from the single line text variable
        textVariable.value = '';
    }
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi