OGeza
Tera Expert

System Properties are used in a lot of different scenarios and ways. In this article I will provide some copy-paste ready boilerplate examples for the most common use cases. For the easiest understandability, I will also color-code the interconnecting code parts (idea by @Shawn Dowler  ).

 

Store data as a JSON, and parse it when you use it:

System Property:
name: group_ids
type: string
value: {"first": "e68f6c022ff001102a71dcb6f699b65e","second":"504fac022ff001102a71dcb6f699b615"}

Script Include:  

var queryGroups = JSON.parse(gs.getProperty('group_ids','<Value if property is not found>');
  
var grGetGroupMembers = new GlideRecord('sys_user_grmember');
    grGetGroupMembers.addQuery('group', queryGroups.first).addOrCondition('group', queryGroups.second);
    grGetGroupMembers.query();

var members = [];
while (grGetGroupMembers.next()) {
  members.push(grGetGroupMembers)
}

 

Store data separated with commas, and convert it to array when you use it:

System Property:
name: group_ids
type: string
value: e68f6c022ff001102a71dcb6f699b65e,504fac022ff001102a71dcb6f699b615

Script Include:

var members = [];
var queryGroups = gs.getProperty('group_ids','<Value if property is not found>').split(',').map(String.trim);

var grGetGroupMembers = new GlideRecord('sys_user_grmember');
    grGetGroupMembers.addQuery('group','IN', queryGroups);
    grGetGroupMembers.query();

while (grGetGroupMembers.next()) {
    members.push(grGetGroupMembers);
  }

 

If you have any other often used use cases, add them to comments, and I will extend the article with it.
Cheers!

 

Please mark this Article as Helpful if it helped you!
Comments
_ChrisHelming
Tera Guru

Nice! For your second example, I'm always worried that someone is going to enter a space in their comma-separated list and that space will end up in the array.

E.g., 

var property = '1,2, 3, 4, 5';
property.split(',');

// returns
 [
  "1",
  "2",
  " 3",
  " 4",
  " 5"
]

 

After the split I usually send the array right to map with String.trim to get rid of those spaces.

property = '1,2, 3,4, 5';
property.split(',').map(String.trim);

// returns
[
  "1",
  "2",
  "3",
  "4",
  "5"
]
Jorn van Beek
Tera Guru

I see the second imput property is rarely used but for me so important to make code robust

like:

var queryGroups = gs.getProperty('group_ids','<Value if property is not found>');

This way you have a way of handling things if a property does not exist. You see ServiceNow using this in default behaviour is A unless you have property Y and set to X then the behaviour changes to B.

Also you ensure you code does not break when someone renames or even deletes the property.

OGeza
Tera Expert

Thanks for the feedback, I added the trimming to the article.

OGeza
Tera Expert

Thank you for the input, I added the 2nd property to the article as well.

Version history
Last update:
‎01-19-2022 06:41 AM
Updated by: