Snippets

Useful snippets of code given as examples to help with various scripting needs.

Scripted SOAP Service

Use this script to add a SOAP endpoint to a Unifi Process. You will need to update the api_name to be the same as the one you set in the Process record.

(function scriptedWebServiceOperation(request, response) {

  var helper = new x_snd_eb.SoapHelper('api_name'); // the API name of the Unifi Process
  var xml = helper.processRequest(soapRequestXML);
  response.soapResponseElement = snd_eb_util.getSoapResponseElement(xml);

})(request, response);

Removing Namespaces

Sometimes it's necessary to remove the namespaces sent in by other systems to make it easier to handle the payload in ServiceNow. The easiest place to do this is within the Scripted SOAP Service, but bear in mind that your payloads will no longer be identical to what the other system sent you. This example shows you how:

(function scriptedWebServiceOperation(request, response) {

  var helper = new x_snd_eb.SoapHelper('api_name'); // the API name of the Unifi Process
  
  // specify the namespaces to strip
  var request_xml = removeNamespacePrefix(soapRequestXML, 'urn:Unifi_RemedyCase_Service');
  
  var response_xml = helper.processRequest(request_xml);
  response.soapResponseElement = snd_eb_util.getSoapResponseElement(response_xml);
  
})(request, response);

function removeNamespacePrefix(payload, namespace) {
  var matcher = new RegExp("xmlns(:[a-z0-9]+)?=[\"']" + namespace);
  var match = payload.match(matcher);
  var replacer;
  payload = String(payload);
  if (match) {
    payload = payload.replace(new RegExp("xmlns" + match[1]), 'xmlns');
    replacer = new RegExp("(</?)" + match[1].substr(1) + ":", "g");
    payload = payload.replace(replacer, '$1');
  }
  return payload;
}

Scripted REST API

Use this script to add a REST endpoint to a Unifi Process. You will need to update the api_name to be the same as the one you set in the Process record.

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    x_snd_eb.ws_console.execute('Request received', function() {
        var helper = new x_snd_eb.RestHelper('incident_guide'); // the API name of the Unifi Process
        processAttachment(helper.getRequest().getRecord());
        helper.processRequest(request, response, 'POST'); // the HTTP method of the resource
    });

})(request, response);

By wrapping the code in console, we give context to Activity Log and prevent multiple database updates.

Poll Requests

Bond location and data

This script shows how to find a bond based on an external reference and store some data.

var integration = poll_request.$model.getIntegration();
var bond = new Bond(integration.getConfig());
var external_ref = 'BP-41951';

try {
  if (bond.locateReference(integration, '', external_ref, 'external')) {
    // found bond ... do your processing here

    bond.setData('key', 'abc');
    bond.setDataObject('key', {foo: 'bar'});

  } else {
    throw 'Unable to locate bond for reference "' + external_ref + '"';
  }
} catch (e) {
  log.error(e);
}