Christopher_Mal
ServiceNow Employee
ServiceNow Employee

This may already be common knowledge, but I thought I would share anyways. I recently attended the pilot class for our Application Development Training (that will be offered at Knowledge 13). One of our ServiceNow developers in attendance used a couple of cool script techniques I thought I would capture here for all to share.

I cannot take any credit for this (except for posting it here). All credit goes to John Maher.

Our requirement was to use an outbound soap message to query another ServiceNow instance for an asset based on a tag. He put this code into a script include called AssetSoapUtil (found below):



var AssetSoapUtil = Class.create();
AssetSoapUtil.prototype = {
initialize: function() {
},

getAsset: function(tag) {
var s = new SOAPMessage('PWX', 'getRecords');
s.setStringParameter('u_asset', tag);
var response = s.post();

return soapXmlToAsset(response);


function soapXmlToAsset(xml) {
var obj = new XMLHelper().toObject(xml);
var asset = obj["SOAP-ENV:Body"].getRecordsResponse.getRecordsResult;
return asset;
}

},

type: 'AssetSoapUtil'
}


What I found fascinating was the function soapXmlToAsset(xml) in which he uses the XMLHelper function to deserialize the response into an asset object. This can be used on any response record result.

You can use this code to describe the object:


var asset = new AssetSoapUtil().getAsset('sometag');
gs.log( '\n-- dump obj --\n' + JSUtil.describeObject(asset, 'ass') );
gs.log( '\n\n-- JSON String --\n' + new JSON().encodeObject(asset) );


There you go. I thought I should share the wealth. We are a community, so hopefully someone out there can make use of this. Thanks again to John.

2 Comments