Comparing variables in each record of a multi row variable set (MRVS)

LikeARabbit
Giga Expert

Working on modifying a workflow script that deals with a multi row variable set (MRVS) and I'm stumped.

I need to compare two values from each row of the MRVS, if any of them return false then I can stop the loop and return a false value. In my MRVS I have several variables, I only need to compare two of them:

  • po_line_item_cost
  • po_line_item_category

If po_line_item_cost is greater than or equal to 1000 then po_line_item_category must begin with 150 (or match a choice in a predetermined list).

So with all of this in mind I need to do a few things:

  • Retrieve each row of the MRVS
  • Determine if po_line_item_cost is greater than or equal to 1000
  • If TRUE, parse the po_line_item_category to see if it either begins with 150 or is included in a list of predetermined options
  • Repeat for each row until there are no more rows or FALSE is returned

This is the current script I'm using, it is only checking for the existence of a 150 category in all of the rows. It does this by concatenating all of the po_line_item_category values into a string then checking to see if 150 exists in that string.

answer = ifScript();

   function ifScript() {

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

// Join array results to form a string
    var mrvsString = mrvs.map(function(mrvs){
      return mrvs.po_line_item_category;
    }).join(",");
      gs.print(mrvsString);

// Search string for CapEx category (150xxx)
    if(mrvsString.indexOf("150")>-1){
      gs.print("Found it!");
      return 'yes';
     }

    gs.print("Nope!"); 
    return 'no';
   }



 

In DEV I've managed to concatenate the category and cost together but I'm coming up empty on how to run the values through the aforementioned logic. I have a feeling I'm missing something obvious.

1 REPLY 1

LikeARabbit
Giga Expert

Actually, I just figured out the looping part. Right now I have this and it's working to compare the two values in each row, I think I can adapt this as needed now.

 

var mrvs;
var itemID = 'insert sys_id of ritm here';
var ritmGR = new GlideRecord('sc_req_item');
if (ritmGR.get(itemID)) {
    mrvs = ritmGR.variables.po_line_items;
}
gs.print(mrvs);

var rowCount = mrvs.getRowCount();
for (var i = 0; i < rowCount; i++) {
  var row = mrvs.getRow(i);
  var category = row.po_line_item_category;
  var cost = row.po_line_item_cost;
    if(cost >= 1000 && category.indexOf("150")>-1) {
        gs.print("Returned TRUE");

      }
    else {

    gs.print("Returned FALSE"); 
    }


  gs.print(category + ', ' + cost + "\n");
}

 

With my test record that's returning the following:

*** Script: Returned FALSE
*** Script: 63130 Professional Svcs - Other, 8000
*** Script: Returned FALSE
*** Script: 13210 Software Maintenance/Support, 2000
*** Script: Returned TRUE
*** Script: 15025 Office Hardware / Equipment, 15000

 

So now, my question is "how can/should I compare those results to get the final pass/fail condition result to determine how the workflow will proceed?"

Do I just process until it finds a FALSE result, or is there a better way?