-
Notifications
You must be signed in to change notification settings - Fork 10
Reusable helpers
Initially, we will provide reusable helper functions to have an easy way for a postback to the servicenow instance. Since we are able to run async code in nodejs, the response to the request(Instance => Node vm) may not come right away, which is fine. This page will provide some reusable code snippets, which you can use in the meanwhile to post back results to the servicenow instance through a scripted rest api which is provided inside the Scoped App for ease of use.
Let's assume we have a script that does some calculations, makes some requests to some other tools/API's. At the end, we will have some data object. We would now like to get this back to the instance, so we can use it somewhere on the platform.
A possible postback script could look like this. Attention, this is purely for inspirational purposes. You are free to do it as you would like to.
We will use the request package here
const request = require('request');
const doPostBack = (logID, data) => {
// we will use the Servicenow REST API
// I choose to use the basic authentication and a user with web service access
// this is a custom scripted endpoint
const baseUrl = "https://user:[email protected]";
const endpoint = baseUrl + "/api/x_321937_snc_node/postback";
request.post(endpoint, {
// we need to send the logID here, this is how the endpoint will identificate the log entry
// the data can be an object or array, the 'json' annotates that the content will be a JSON string
json: {
logID: logID,
result: data
}
}, (error, res, body) => {
// the error message could be sent back here too if needed, it is up to you
if (error) {
console.error(error);
return
}
console.log(`statusCode: ${res.statusCode}`);
console.log(body)
})
}The above would take two parameters, the first one is the logID you can get this value by calling the internal function _getLogID() see Internal functions.
The second is the data or the result, so anything you would need on the instance to be available.
Note: We will have to find some more user-friendly way to do this. Suggestions welcome!
Servicenow x NodeJS @ 2019