LogoLogo
AboutSupport
4.2
4.2
  • Unifi User Documentation
  • Install
    • Release Notes
      • Unifi 4.2 Release Notes
      • Unifi 4.1 Release Notes
      • Unifi 4.0 Release Notes
      • Unifi 3.1 Release Notes
      • Unifi 3.0 Release Notes
      • Unifi 2.2 Release Notes
      • Unifi 2.1 Release Notes
      • Unifi 2.0 Release Notes
    • Install or Upgrade
      • Global Utility
      • Hotfix
  • Configure
    • Integration Designer
    • Processes
    • Integrations
    • Connections
    • Messages
    • Message Scripts
    • Fields
    • Field Maps
    • Response Actions
    • Event Actions
    • Datasets
      • Create a New Dataset
      • Dataset Extras
    • Polling
      • Pollers
      • Poll Processors
    • Administration
      • Activity Logs
      • Data Stores
      • Properties
      • Scheduled Scripts
      • System Logs
    • Attachments
      • Extracting Attachments
      • Fetching Attachments
      • Embedded Attachments
    • Scripting
      • Snippets
      • Variables
    • Documentation
    • How to guides
      • How to Handle Attachments
        • Message
        • Scripted REST Resource
        • Test AddAttachment
      • How to Manually Close a Bond
      • How to Poll for Large Response Payloads
      • How to Setup an OAuth Connection
        • Identity Provider Instance
        • Identity Consumer Instance
        • OAuth Refresh Token Job
      • How to Setup Heartbeat Messages
  • Deploy
    • Package
    • Instance Clone
  • Operate
    • Bonding
      • Bonds
      • Bonded Attachments
    • Transport
      • Snapshots
      • Transactions
      • Stages
      • HTTP Requests
      • Dataset Requests
      • Poll Requests
    • Error Handling
      • Integration Pause and Resume
      • Integration Repair
      • Request Retry
      • Transaction & Request Replay
      • Transaction Ignore
  • Test
    • Overview
    • Integration Test
    • Test Scenario
    • Test Scenario Data
    • Test Result
    • Test Scenario Result
    • Generating Tests
    • Running Tests
    • Exploring Results
  • Integration Guides
    • Outbound Incident Guide
      • Getting Started
      • Process
      • Integration
      • Connection
      • Create Scenario
        • CreateIncidentResponse Message
        • CreateIncidentResponse Fields
        • CreateIncident Message
        • CreateIncident Fields
        • Trigger
        • Test CreateIncident
      • Update Scenario
        • Response Message
        • UpdateIncident Message
        • UpdateIncident Fields
        • Test UpdateIncident
      • Resolve Scenario
        • ResolveIncident Message
        • ResolveIncident Fields
        • Test ResolveIncident
      • Build - Integration Level
      • Conclusion
    • Bidirectional Asynchronous Incident Guide
      • Getting Started
      • Process
      • Web Service
      • Integration
      • Connection
      • Create Scenario
        • Response Message
        • CreateIncidentReceipt Message
        • CreateIncidentReceipt Fields
        • CreateIncident Message
        • CreateIncident Fields
        • Trigger
        • Test CreateIncident
      • Update Scenario
        • Receipt Message
        • UpdateIncident Message
        • UpdateIncident Fields
        • Test UpdateIncident
      • Resolve Scenario
        • ResolveIncident Message
        • ResolveIncident Fields
        • Test ResolveIncident
      • Build - Integration Level
      • Build the Other Half
        • Move the Integration
        • Reconfigure the Connections
      • Conclusion
    • Incident Update Poller Guide
      • Polling
        • Poll Processor
        • Poller
      • Inbound Message
        • UpdateIncidentInbound Message
        • UpdateIncidentInbound Fields
      • Message Identification
      • Bond Identification
        • Edit Incident Form
        • Edit CreateIncident Message
      • Test Update Poll
      • Conclusion
    • Incident Multiple Message Poller Guide
      • Polling
        • Poll Processor
        • Poller
      • Inbound Messages
        • ResolveIncidentInbound Message
        • ResolveIncidentInbound Fields
      • Testing
        • Test UpdateIncidentInbound
        • Test ResolveIncidentInbound
      • Conclusion
    • Incident Create Poller Guide
      • Polling
        • Connection Variables
        • Poll Processor
        • Poller
      • Messages
        • CreateIncidentInboundReceipt Message
        • CreateIncidentInboundReceipt Fields
        • CreateIncidentInbound Message
        • CreateIncidentInbound Fields
      • Build - Integration Level
      • Test Create Poll
      • Conclusion
    • Incident Parent and Child Poller Guide
      • Polling
        • Connection Variables
        • Child Poll Processor
        • Child Poller
        • Parent Poll Processor
        • Parent Poller
      • Inbound Messages
      • Testing
        • Test UpdateIncidentInbound
        • Test ResolveIncidentInbound
      • Conclusion
    • Incident Attachment Poller Guide
      • Polling
        • Connection Variables
        • Edit Endpoint URLs
        • Get Attachment Poll Processor
        • Get Attachment Poller
        • Select Attachments Poll Processor
        • Select Attachments Poller
        • Edit Child Poll Processor
        • Edit Child Update Poller
      • Messages
        • AddAttachmentInbound Message
      • Testing
        • Test Outbound Scenarios
        • Test CreateIncidentInbound
        • Test UpdateIncidentInbound
        • Test ResolveIncidentInbound
        • Test AddAttachmentInbound
      • Conclusion
  • Troubleshooting
    • Attachments
      • Inbound SOAP/Base64 attachments stopped working
      • New record attachments are not sent from Portal
      • Special characters in attachment file names
    • Datasets
    • Development
      • Bonding to existing records
      • Copying an existing Unifi trigger rule doesn't work
      • Duplicate messages being sent
      • Deleted records are not packaged
      • Multipart Form Data
      • Undefined error when building an integration
    • Diagnostic
    • Installation
      • Latest version of Unifi not accessible
    • Integration Responses
      • Transaction has been processed already
      • Initiating transaction not found for inbound receipt
      • Message has already been processed
      • Message ID not found
      • Message is not valid for this bond
      • Message name not recognised
      • No retry for requests with 401 response
      • Unable to identify message name from request
    • Other
      • Dynamic stage does not render
      • Duplicate bonds on Request integrations
    • Self-test
  • About
    • Quick Tour
    • Supported Features
    • Application Modules
    • Data Model
    • Transport Data Flow
Powered by GitBook
On this page
  • Scripted SOAP Service
  • Removing Namespaces
  • Scripted REST API
  • Poll Requests
  • Bond location and data

Was this helpful?

Export as PDF
  1. Configure
  2. Scripting

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;
}
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
    <soapenv:Body>
        <ns0:CreateServiceNowCase xmlns:ns0="urn:Unifi_RemedyCase_Service">
            <ns0:Message>
                <ns0:SourceMessageID>TXN0001001</ns0:SourceMessageID>
                <ns0:Timestamp>2022-10-27T15:01:41Z</ns0:Timestamp>
                <ns0:SourceReference>00001337</ns0:SourceReference>
            </ns0:Message>
            <ns0:Detail>
                <ns0:RequestedBy>Lance Armstrong</ns0:RequestedBy>
                <ns0:Description>This is a test</ns0:Description>
                <ns0:Impact>5</ns0:Impact>
                <ns0:ShortDescription>Testing with Unifi</ns0:ShortDescription>
            </ns0:Detail>
        </ns0:CreateServiceNowCase>
    </soapenv:Body>
</soapenv:Envelope>
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
    <soapenv:Body>
        <CreateServiceNowCase xmlns="urn:Unifi_RemedyCase_Service">
            <Message>
                <SourceMessageID>TXN0001001</SourceMessageID>
                <Timestamp>2022-10-27T15:01:41Z</Timestamp>
                <SourceReference>00001337</SourceReference>
            </Message>
            <Detail>
                <RequestedBy>Lance Armstrong</RequestedBy>
                <Description>This is a test</Description>
                <Impact>5</Impact>
                <ShortDescription>Testing with Unifi</ShortDescription>
            </Detail>
        </CreateServiceNowCase>
    </soapenv:Body>
</soapenv:Envelope>

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);
}
PreviousScriptingNextVariables

Was this helpful?