Montag, 13. November 2017

Service Now Script Includes: Make sure to convert object references to values

Because I want to have a clean design, I try to use System properties. This gives me flexibility and clarity over strange literals like

var group_sys_id = "690acf664fc51200d4a679a18110c746" ;
Instead I write
var group_sys_id = gs.getProperty("inc.assignment.group.default") ;
Even if you have never seen one of my instances, it will be clear from the code above that the property defines the sys_id of the default assignment group.

Sometimes, you have to deal with a list of properties and you might not know initially how many there are. An example we encountered: the customer wants to send out notifications for expiring firewall rules. Currently, there are notifications to send 30, 14 and zero days prior to expiry.
The worst solution for this would be to add 3 columns to the table where said firewall rules are stored and put the actual notification dates there.
This is bad on many levels:

  • It is a violation of good database design practise (Violation of the 1st Normal Form)
  • It leads to a loss of flexibility (only an admin can add more notifications)
  • You assume that there will always be three notifications
Therefore, I create a naming convention and use it to name a couple of system properties. In my case 
  • sc.firewall.notify.days.1
  • sc.firewall.notify.days.2
  • sc.firewall.notify.days.3
sc because this concerns the service catalog. The values just contain integers (30, 14, 0).
All very well, now I just need a function (in a script include) to return these values.

How not to do it

My first attempt looked simple enough: But when I tested it (you always do that, right?), I was surprised (running this in a background script).

var Toolbox = new Toolbox_Functions()
var res= Toolbox.getPropertyList("sc.firewall.notify.")
gs.print(res);
[0:00:00.002] Script completed in scope global: script
*** Script: sc.firewall.notify.3,sc.firewall.notify.3,sc.firewall.notify.3
Whoa what's that?

Fixing the Problem

This is not a problem with Service Now, but a problem with JavaScript (or even Object-Oriented programming). The reason for this behaviour is that property_gr.name (line 23) contains an object reference and it will always point to the last element read (that is why we get the third entry three times)
GlideRecord to the rescue: Service Now has added the function getValue(), well, to get the value (instead of the default object reference).
Another approach would be to use

arr.push(property_gr.name.toString());

or

arr.push(String(property_gr.name));


Recommended Reading

Keine Kommentare:

Kommentar veröffentlichen