Array Question

Derek10
Tera Expert

I have a scenario, where I have 30 variables and 3 outcomes.

I'm trying to find the best way to make this as dynamic as possible. They are all check boxes. Basically I'll be looking at 1-15 if any are true, do this, 16-30   if any are true do that, if none are true, go here.

Doing a run script as a switch because multiple outcomes can happen concurrently.

I could use abit of help with the syntax. I looked up the arrayutil in the wiki which I think will help as it does the contains option. ArrayUtil - ServiceNow Wiki

But i'm not sure how to setup the array using vars and telling it to search through all 15 at a time. I'm assuming I manually have to enter them into the array.

Any thoughts out there? This is on the workflow side for clarity.

21 REPLIES 21

I did a proof of concept here, I'll try to explain with comments as I go:



If you only care about when the options are true, then I would just include that in the query and get rid of the if on line 16.



var arrayUtil = new ArrayUtil();



//The variable question field is what I compare the checked options against


var groupOne = ['test_1','test_2'];


var groupTwo = ['test_3'];



var gr = new GlideRecord('sc_item_option_mtom');


gr.addQuery('request_item','cb28f52f4f3192001fa822d18110c7fc');


//Only check "Checkbox" options


gr.addQuery('sc_item_option.item_option_new.type','7');


gr.orderBy('sc_item_option.order');


gr.query();



while(gr.next()){


    //Only run our logic if we get checked values


  if (gr.sc_item_option.value == 'true'){


          if (arrayUtil.indexOf(groupOne, gr.sc_item_option.item_option_new.name)){


          //Group one logic


          gs.print("Group one");


          } else if (arrayUtil.indexOf(groupTwo, gr.sc_item_option.item_option_new.name)){


          //Group two logic


          gs.print("Group two");


          } else {


          //Logic when it is in neither group.


          gs.print("Group three");


          }


  }


}



You can paste this in a background script if you want to try or tweak it.


Another very interesting approach which would work in an myraid of situations, since I have seperate subsets of data and am trying to avoid listing out all the vars (which can keep it dynamic) I am going to first try the previously Stephens approach. Technically both your answers are correct as there is many ways to skin this particular cat.



I appreciate the efforts of both of you and am very impressed at the level of detail that everyone on this thread has progressed to. I hope this will be useful to others and not just myself as these scenarios are most likely to be encountered elsewhere.




Times like these really make me glad that we got off BMC!