How can I loop through the choices on a system property?

Michael Murphy
Tera Expert

Hi Everyone,

How do I loop through the choices in a system property?  I have a script include which populates a list of Hardware assets.  What I then want to do is is check the model category of the asset against the choices in a system property and only display the ones that match.

So for example if the system property has the choices of desktop,Laptops,smartphones.  I want to be able to check the Model Category of each returned asset to see if it matches one of the three choices so the list shows only Desktops, Laptops and Smartphones.

I know I can use gs.getProperty() to get the value but how do I cycle through the choices?

1 ACCEPTED SOLUTION

Anurag Tripathi
Mega Patron
Mega Patron

HI,

One way would be to do something like below

var str = gs.getProperty('<your Property>');
var arr = str.split(',');
for(var i=0;i<arr.length;i++)
{

//arr[i]  gives you one value from property, like smartphone, do whatever with it.
}
-Anurag

View solution in original post

4 REPLIES 4

Anurag Tripathi
Mega Patron
Mega Patron

HI,

One way would be to do something like below

var str = gs.getProperty('<your Property>');
var arr = str.split(',');
for(var i=0;i<arr.length;i++)
{

//arr[i]  gives you one value from property, like smartphone, do whatever with it.
}
-Anurag

Ankur Bawiskar
Tera Patron
Tera Patron

@Michael Murphy 

something like this

var propertyValue = gs.getProperty('model.category.choices');

var rec = new GlideRecord('alm_hardware');

rec.addQuery('Your query here');

rec.query();

while(rec.next()){

var value = rec.model_category.getDisplayValue();

if(propertyValue.indexOf(value) > -1){

gs.info('Matching one of the choices from property value');

}

}

find_real_file.png

Regards
Ankur

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

Michael Murphy
Tera Expert

Thanks for the answers guys.  I will try them out and let you know.

Michael Murphy
Tera Expert

Thanks again guys i have tried the solutions and i think @Anurag Tripathi's solution will fit in nicely as I need to work it into an existing script include.

I think the key to understanding this is that it is easier to use the system property value as a comma separated list than it is to put that data into the choices field.  Then use that to populate an array.

Still not sure but maybe the choices are meant to be cycled through or used to set the property or if they are simply intended to just populate a choice list with generic choices.

Thanks again guys for the speedy answer even though I'd clocked off for the day 🙂