Servicenow Extensions
Interesting ideas, solutions to various problems you might face during development
While working on new features and writing scripts for the platform, there are times where you will do the same thing repetitively. This is of course good to train your mind to remember part s of code easily, but also produces a lot of excessive lines of code which would result in less readability in the long term, produce “longer” scripts and make the whole thing harder to maintain.
We will write various snippets or code samples, sometimes extensions to core things that would make it easier to have a more-readable, more user-friendly code to work with in a longterm. Also this would provide stability of methods/features and make it easier in the future to adapt to new core changes in the servicenow releases.
With our extensions we provide stability, through apidoc’s. API versioning for classes used across the platform. This provides a possibility to use more versions at once and also we will focus on backward compatibility.
Our improvements/extensions will be provided in many ways. Sometimes introducing a new class, providing code snippets or even extending the base functionality if appropiate. We will strive to provide powerful tools to make the life of developers easier.
GlideRecordExt
The name of the script may change if we find something better, but for now lets stay with this one.
We use GlideRecord probably on a daily basis while creating new functionality for the platform. This is a core class which is used for communicating with the database through the platform. While it is a powerful tool, it lacks some modern way of handling the data sometimes. See GlideRecordExt.js on Github
To provide some examples on what this can do and change the code we have in place. Most of the time, we would like to get an array of sys_id’s for using it in our code, with GlideRecord, the code would like this:
var result = [];
var gr = new GlideRecord("incident");
gr.query();
while(gr.next()) {
result.push(gr.sys_id + "");
}
// now we would use the result as we would like to
While using the GlideRecordExt, we could have the same effect in less lines and more readability:
var gr = new GlideRecordExt("incident");
gr.run();
var result = gr.getSysIDArray();
// we have here the same result with only 3 lines,
// in the background, we also check for the query
// if it has any results to iterate at first,
// so if there are no results, we will not
// even do the while loop and get back an empty array
This simple example demonstrates the ease of use and readability which is our main reason to provide these kind of extensions. There are of course more samples available at the repository, please take a look at the wiki page https://github.com/0x111/servicenow-extensions/wiki
We will provide more and more methods like this to provide easy and more readable ways to create code for the platform, so stay tuned and follow the repo.
Also please provide any feedback regarding improvements or changes to the current methods, if they are constructive, they are all welcome.