Posted in: ServiceNow

A better “Scripts – Background”

At some point (about the 2nd time I had to use Scripts – Background) I got sick and tired of the textbox in which I was supposed to edit and test server side JavaScript. I said to myself if the build-in code editor works everywhere else, it must work there too. So I started researching what made the built in editor tick and eventually adapted the GlideEditor to work on “Scripts – Background”.

The solution requires installing Tampermonkey (Greasemonkey won’t do) and the user script I came up with. Put the two together and you can have the platform code editor working on “Scripts – Background”. It provides most of the editing aids the original one does like code formatting (Ctrl+O), searching, searching for next occurrence (F3), replacing, making selected text lower case (Ctrl+L), making it upper case (Ctrl+U). It also provides help: pressing Ctrl+I pops up a small help tip containing an anchor to documentation. For platform API the anchor points to the official docs page documenting the selected token, for JavaScript API the anchor points to the relevant MDN page.

But to reach full capabilities one needs to install a few processors in instances where the tool is to be used. With the help of those the editor can provide field names for GlideRecord objects declared with constant table names, is capable of providing dot-walking code completion. E.g. declaring

var task_gr = new GlideRecord('task');

will make the editor show all fields of task when the dot operator is inserted:

As you can see it offers not only the standard GlideRecord API members but also fields of the table – with explanation. You can also observe a couple of interesting things: linting also works and you have line number and last column indicator. Now showing dot-walking capability:

As opposed to the platform editor, this one understands and suggests JavaScript API too:

If you would like to give it a try install Tampermonkey and the user script. If you want to try out the enhanced code completion capabilities you need to install the processors. Import the latter as XML.

Know that this is by no means a polished or finished product. It is still very much a work in progress, not guarantied to work flawlessly. While I cannot imagine living without it ( 🙂 ) standard disclaimers apply.