sn-node -- ServiceNow Node Client with GlideAPI
sn-node is a ServiceNow client that extends the GlideAPI to Node as well as providing standard HTTP GET and POST functions
Logging In
To log in simply include sn-node and create an instance to your...err...instance. An instance of your instance.
It is not necessary to put the whole instance name, just the prefix will work.
For example if you go to https://sniscool.service-now.com, then your instance name is 'sniscool'
var ServiceNow = ;var my_instance = "instance name" "user name" "password";my_instance;
Basic Request
Once logged in you can now make requests to pages. Here is a simple request to get home.do
instance;
You can also do a post to submit forms.
instance;
You may also download files by specifying a file name for either get or post.
instance; instance;
GlideRecord
The GlideRecord object has been fully replicated and even extended in sn-node. You can use the functions as you normally would however since node executes differently than the browser or the ServiceNow server you must specify call backs for requests. This will only affect query, update, insert, delete, get.
This is a simple GlideRecord request. Notice to create a new instance of GlideRecord you must use the instance object.
var newRec = "incident";newRec;newRec;
The callback function passed to query will receive the same object back as a parameter so you can use it just like you would normally except it will just be in the callback.
var newRec = "incident";newRec;newRec;
You may also do inserts as you normally would. The callback function for insert will receive the new sys_id of the created record.
var newRec = "incident";newRecpriority = "1";newRecshort_description = "New incident from SN Client Node";newRec;
The get function accepts two parameters, the first is the sys_id as usual, the second is the callback, which will receive the current GlideRecord object back, which can then be used like normal.
var newRec = "incident";newRec;
Updates will also work. Here is a more complex example, in order for the code to execute in the correct order, the subsequent actions are nested within the callbacks. This example makes two calls, the first to get the record and one more to update it.
var newRec = "incident";newRec;
Here is another example updating an entire GlideRecord object.
var newRec = "incident";newRec;newRec;
Finally, I have also added a gotoID function to quickly go to a record with a specific ID. This could be useful if you keep a list in cache and need to use it to look up referenced fields as dotwalking will not work.
This example just queries for all incidents and then goes directly to the record with the given ID.
var newRec = "incident"; newRec;
GlideAJAX
GlideAJAX has been replicated although the functions to do a synchronous call have been removed since they do not fit into node.
Here is a simple example of a simple AJAX call to a Script Include. Note: Script Includes will need to be set to Client Callable as the request is executed as a client.
var ga = "RemoteTest";ga;ga;
Notice now to make the request it is simply makeRequest and the function always takes a callback.
Notes on structure
In order to simplify requests sn-node uses a queue for transactions. This means a single request can be made per instance object. When new requests are made they are actually added to the queue and are executed in order received.
So for claritys sake here is a snippet with annotations showing what order the requests will be made
var ServiceNow = ;var instance = "myinstance" "admin" "password";// 1 - request to logininstance; // 2 - request to home.doinstance; var newRec = "incident";// 3 - GlideRecord to incident tablenewRec; var anotherRec = "incident";// 4 - GlideRecord to get incident recordanotherRec;
Callbacks
The use of callbacks is not common within ServiceNow and will take some getting used to. The callback function will be executed whenever the calling function is complete. Node does not wait around and moves on to the next line. You can never be certain which will actually be finished first and because of this, if you are dependent on a call being finished you must put the dependent code in a callback.
Feedback
You are free to use this library in any project you want persuant to the license. I welcome any feedback or suggestions for enhancements. To log an issue please use the Issues feature in Github.
My current plans for the library to include better error handling and continue adding functionality.