Created
November 29, 2019 17:58
-
-
Save thisnameissoclever/6121572292813917534d0fcbbf23def4 to your computer and use it in GitHub Desktop.
Useful ServiceNow Scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var exampleObj = { | |
"name": "Tim", | |
"age": 31, | |
"groovy": true, | |
"dogs": [ | |
{ | |
"name": "Ezri", | |
"age": 1 | |
}, | |
{ | |
"name": "Seven", | |
"age": 3 | |
} | |
] | |
}; | |
gs.info( | |
'Logging JSON objects can be very useful, but they need to be stringified first. If we ' + | |
'just stringify them in the usual way, we get something that\'s kind of hard to read, like this: \n' + | |
'{0}\n\n' + | |
'But, we can do better, by specifying some additional parameters to the .stringify() method like ' + | |
'so: JSON.stringify(exampleObj, null, 2) : \n{1}', | |
JSON.stringify(exampleObj), | |
JSON.stringify( | |
exampleObj, | |
null, //A function or array to filter/replace some values of the object (useful if you want to log an object, but not log potentially sensitive elements) | |
2 //The number of spaces by which to indent child-elements. You can also specify a string or escape character such as '\t' for tabs. | |
) | |
); | |
/*Output: Logging JSON objects can be very useful, but they need to be stringified first. If we just stringify them in the usual way, we get something that's kind of hard to read, like this: | |
{"name":"Tim","age":31,"groovy":true,"dogs":[{"name":"Ezri","age":1},{"name":"Seven","age":3}]} | |
But, we can do better, by specifying some additional parameters to the .stringify() method like so: JSON.stringify(exampleObj, null, 2) : | |
{ | |
"name": "Tim", | |
"age": 31, | |
"groovy": true, | |
"dogs": [ | |
{ | |
"name": "Ezri", | |
"age": 1 | |
}, | |
{ | |
"name": "Seven", | |
"age": 3 | |
} | |
] | |
}*/ | |
//More on the stringify method here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @description Retrieve the "body" of a "text/plain" (or other unsupported Content-Type) | |
* inbound REST request, via a SRAPI (Scripted REST API). | |
* @param {RESTAPIRequest} restRequest - The inbound request body from the Scripted REST | |
* API (SRAPI). | |
* @returns {string} The body of the inbound request in a plain-text string. | |
* @private | |
*/ | |
function getPlaintextBody(restRequest) { | |
var bodyLine; | |
var bodyText = ''; | |
var reqBody = restRequest.body; | |
var streamBody = reqBody.dataStream; | |
var gtrBodyReader = new GlideTextReader(streamBody); | |
//get first line | |
bodyLine = gtrBodyReader.readLine(); | |
while (typeof bodyLine == 'string') { | |
bodyText += bodyLine + '\n'; | |
//get next line | |
bodyLine = gtrBodyReader.readLine(); | |
} | |
return bodyText; | |
} | |
/****************** | |
* EXAMPLE USAGE | |
*******************/ | |
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) { | |
var stringBody = _getPlaintextBody(request); | |
gs.debug('Message reqBody: \n' + stringBody); | |
/** | |
* @description Retrieve the "body" of a "text/plain" (or other unsupported Content-Type) | |
* inbound REST request, via a SRAPI (Scripted REST API). | |
* @param {RESTAPIRequest} restRequest - The inbound request body from the Scripted REST | |
* API (SRAPI). | |
* @returns {string} The body of the inbound request in a plain-text string. | |
* @private | |
*/ | |
function _getPlaintextBody(restRequest) { | |
var bodyLine; | |
var bodyText = ''; | |
var reqBody = restRequest.body; | |
var streamBody = reqBody.dataStream; | |
var gtrBodyReader = new GlideTextReader(streamBody); | |
//get first line | |
bodyLine = gtrBodyReader.readLine(); | |
while(typeof bodyLine == 'string') { | |
bodyText += bodyLine + '\n'; | |
//get next line | |
bodyLine = gtrBodyReader.readLine(); | |
} | |
return bodyText; | |
} | |
})(request, response); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment