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() {
},
/*
* 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 currentTime = 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 currentTime = 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'
}