I have decided to provide a helper library for ServiceNow integrations. This will not be an update set, but rather, it will be a library that you can copy and paste at your leisure and you/modify/update if you wish.
The library will exist in a cloud-based version control tool called “Gist “. As I add new functions to the library, they will be version controlled. When you come to this blog post, you will always be getting the latest version of my helper library.
It is starting off slow, but will grow as I throw stuff in there that I end up always using.
var IntegrationHelper = Class.create();
IntegrationHelper.prototype = {
initialize: function() {
},
/*
* getEccResponse
*
* This method will take the sysID of an output record on the ECC queue
* and wait up to a max number of MS for a corresponding "input"
* response to be generated on the queue.
*
* PARAMS
* outputSysId - The sys_id of the "output" record on the queue
* waitMS - max number of miliseconds to to keep polling for the response.
*
* RETURNS
* null - no response in within the max wait time;
* Otherwise, we return the ECC Queue Payload string from the response record
*/
getEccResponse: function(outputSysId,waitMS){
if(!waitMS) {
waitMS = 25000;
}
var start = new GlideDateTime;
//Loop waiting for a record that is created in response to your query
var resp = new GlideRecord("ecc_queue");
resp.addQuery("response_to", outputSysId);
resp.addQuery("queue", "input");
do{
resp.query();
resp.next();
gs.sleep(1000);
if ( GlideDateTime.subtract(start, new GlideDateTime()).getNumericValue() > waitMS ) {
return null;
}
} while(!resp.sys_id);
//Found a response
return this.getEccPayload(resp);
},
/*
* getEccPayload
*
* Large payload bodies will create an attachment on the ECC Queue record.
* This function will automatically determine if the response is an attachment
* or if it is plain text within the payload field. It will return the full
* payload string regardless of whether it is contained within the field or in
* an attachment.
*
* PARAMS
* ecc - The GlideRecord of the ECC Queue record
*
* RETURNS
* Full payload string
*
*/
getEccPayload: function(ecc){
if(ecc.payload != "<see_attachment/>"){
return ecc.payload;
}
var SysAttachment = Packages.com.glide.ui.SysAttachment;
var sa = new SysAttachment();
var payload = sa.get(ecc, "payload");
return payload;
},
/*
* Similar to the SOAPMessage "getReponse()" function. This method
* will wait a max time polling for responses from the ECC Queue.
*
* PARAMS
* s - The SOAPMessage class instance that submitted the SOAP Request
* waitMS - max number of miliseconds to to keep polling for the response.
*
* RETURNS
* null - no response in within the max wait time; or no successful retries
* Otherwise, we return what is in the ECC Queue Payload on the successful record.
*/
getResponse: function(s,waitMS){
var start = new GlideDateTime;
//GlideDateTime.subtract(start, new GlideDateTime()).getNumericValue() > waitMS
//attempt to get the response on the orig request
var k = 1;
var response = s.getResponse();
while(response == null) {
gs.log("waiting ... " + k + " seconds");
response = s.getResponse(1000);
if(response){
break;
}
k++;
if ( GlideDateTime.subtract(start, new GlideDateTime()).getNumericValue() > waitMS ) {
return null;
}
}
return response;
},
/*
* This method will wait a max time polling for responses from
* retried requests that take place due to a failure.
* Only use this method if you have ascertained that there is a retry
* policy triggered on original request.
*
* PARAMS
* s - The SOAPMessage class instance that submitted the SOAP Request
* waitMS - (optional) max number of miliseconds to to keep polling for the response.
* otherwise we just wait for the retry policy to finish before we give up.
*
* RETURNS
* null - no response in within the max wait time; or no successful retries
* Otherwise, we return what is in the ECC Queue Payload on the last retry
*/
getRetryResponse: function(s,waitMS){
var start = new GlideDateTime;
//GlideDateTime.subtract(start, new GlideDateTime()).getNumericValue() > waitMS
var succeeded = false;
while(!succeeded){
var retry = new GlideRecord('ecc_queue_retry_activity');
retry.initialize();
retry.addQuery("retry_queue_entry", s.soapEnvelope.outputq);
retry.orderByDesc("sys_created_on");
retry.query();
retry.next();
if( retry.status == "succeeded" ){
s.soapEnvelope.outputq = retry.output_queue;
s.soapEnvelope.response_payload = null;
succeeded = true;
var response = s.getResponse();
break;
} else {
if( retry.status == "failed" ){
break;
}
Packages.java.lang.Thread.sleep(1000);
}
if(waitMS && GlideDateTime.subtract(start, new GlideDateTime()).getNumericValue() > waitMS){
//taking longer than we had allotted
return null;
}
}
if(succeeded){
return response;
}
return null;
},
type: 'IntegrationHelper'
}