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.

Querying multi row variable set array

LikeARabbit
Giga Expert

My use case is that I need to route approvals of a requested item based on variables within a multi row variable set (MRVS).

So far I have it working without issue by using the "startsWith" function on records that have a single MRVS row, using the code below.

answer = ifScript();

   function ifScript() {

    var mrvs;
    var itemID = current.sys_id;
    var ritmGR = new GlideRecord('sc_req_item');
    if (ritmGR.get(itemID)) {
        mrvs = ritmGR.variables.po_line_items;
    }

    var rowCount = mrvs.getRowCount();
    for (var i = 0; i < rowCount; i++) {
      var row = mrvs.getRow(i);
      var category = row.po_line_item_category;
   
   // Determine if category begins with 150
      var result = category.startsWith("150");
      gs.print(result);

      if (result == true) {
         return 'yes';
      }
      return 'no';
   }
 }

However, this doesn't work with multiple lines/rows in the MRVS. Instead it appears to query the first row and stop. 

Is there a way to instead query the entire MRVS array for a string, so that it returns TRUE if the string is found on any row and FALSE if the string is not found any any row?

 

1 ACCEPTED SOLUTION

Narendra Kota
Mega Sage

Hi,

Replace the below lines of code like this from he if statement:

if (ritmGR.get(itemID)) {
        mrvs = JSON.parse(ritmGR.variables.po_line_items);
    
    for (var i = 0; i < mrvs.length; i++) {
      var category = mrvs[i].individual_variable_name;

//......................now then if category is some thing
//.....................do something


}
}

Hope this helps.
Mark helpful or correct based on impact.

Thanks.

View solution in original post

5 REPLIES 5

Shiraz2
Mega Guru

I created an Article on MRVS. Please see attached if that helps you.

If it does, please mark my answer accordingly.

Thanks.

 

ShiraZ.