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

Samstag, 15. April 2017

Book review - The Peter principle by Laurence J Peter and Raymond Hull

This is the first installment of a new section of my blog - the review of vintage books.

“The Peter Principle” is known by many, but few have taken the time to actually look at the book that defines the principle. This volume, written in 1969 by journalist Raymund Hull as a popular account of the work of Laurence J. Peter is a fascinating piece of work. Much to my surprise, this book is very tongue-in-cheek and full of self-criticism.

On the surface, the book is based on this idea:

Every employee tends to be promoted until the individual level of incompetence is reached.

Starting from this statement, a wohle new science (the so-called “hierarchology”) is developed and many interesting “real-life” examples are presented.

Great examples include the “flying T-Formation”, where 23 vice presidents lead 3 divisions, the “free floating apex” where “a director is in charge of a non-existing department” or the most modern hierarchy of all where the computer replaces all lower ranks and only managers would remain.

Many examples can be found in real life - my favorite example is the “lateral arabesque”, where a manager is moved out of the way and given a much longer, important-looking job title.

The authors also provide guidance on how to avoid the trap of the principle - for example by developing some eccentric traits, such as leaving all drawers on ones office open when leaving in the evening.

Ultimately, the book has a far wieder scope - Peter and Hull are concerned about mankind as such and urge the reader to prevent our race from begin promoted to far (into oblivion). Here, they speak of “life competence”, which we as a race must retain.

A weak point of the book is its latent sexism. In this aspect, the book seems dated, while the book is modern in most other aspects.

The end of the book is symptomatic of the theme: Peter asks if somebody is willing to fund a professors post for him (because he has proven competent at the PhD level). This humorous self-criticism is a very strong aspect of this book.