Get Attachment Poll Processor

The Poll Processor contains the logic that will be applied when polling a remote system for data.

The Poll Processor is a configuration record which contains the logic that will be applied when polling a remote system for data. There are three main scripts which are used to setup, execute and process the Poll Requests. The scripts given here are examples of how you might configure your Poll. The details of yours may differ depending on your requirements.

This Get Attachment Poll Processor will take take each record passed to it from its parent - Select Attachments Poll Processor - and then make a Poll Request against that record, retrieving the attachment, building a payload and passing it to Unifi to process.

New Poll Processor

In Unifi Integration Designer, navigate to & open < Your Integration > (created following the Outbound Incident Guide).

Click the 'Poll Processors' icon & then New.

The fields to be configured for the New Poll Processor modal are as follows:

#

Field

Description

Value

1

Name

The name of the Processor.

<Your Name>

2

Run parallel

Allow concurrent Poll Requests to be executed.

<true>*

*Run parallel: Each Poll Request will have different input parameters (i.e. the sys_id of one of the attachments passed from its Parent). We can therefore run concurrent Poll Requests on the different records without concern for overlap issues.

Your New Poll Processor modal should look like this:

'Parent' - Once we have created its Parent Poll Processor, we can then select it here. We have not scripted any business logic to run from the value in this field. It is given for documentary purposes only.

3) Submit and view to further configure the Poll Processor.

Setup Script

The Setup Script is the first script to run (it runs at the point in time the Poll Request is created). It is used to build the environment for the poll and define what it will do. We will use it to setup the URL that will be called.

Navigate to Scripts > Setup Script.

The initial Poll Processor fields to be configured are as follows:

#

Field

Description

Value

4

Setup script

The script to setup the Poll Request record.

Update the code in the Setup script field so that it looks like the code below

The code in the Setup script field should look like this:

// Configure the new Poll Request record
(function (poll_request, poller, params) {
// Params contain an attachment object containing details of the remote attachment
// remote_id : The sys_id of the attachment in the remote system
// Also contains: file_name, content_type, size_bytes
var attach_id = params.attachment.remote_id;
poll_request.endpoint_url += '/attachment/' + attach_id + '/file';
})(poll_request, poller, params);

Setup script: The parameters for which data to return are contained in the endpoint url.

Endpoint URL: The value used in the poll_request.endpoint_url was initially generated using the ServiceNow REST API Explorer polling the Attachment API, substituting a variable in for the sys_id of a specific attachment to query. This value is appended to the existing endpoint url in the active connection before being added to the Poll Request.

params: The params object is passed through to the subsequent scripts (and on to further Pollers, if required). This script has been passed the sys_id of a specific attachment from its parent Poller.

Your Setup Script form should look like this:

5) Navigate to Request Script.

Request Script

The Request Script is used to reach into the remote system and execute the request. We will use the ServiceNow RESTMessageV2() web service to make a REST call to the URL defined in the Setup Script.

The next Poll Processor field to be configured is as follows:

#

Field

Description

Value

6

Request script

The script that executes the request.

Update the code in the Request script field so that it looks like the code below

The code in the Request script field should look like this:

// Process the request e.g. by executing a web service and returning the response
(function (poll_request, poller, connection, params) {
var request, response;
request = new sn_ws.RESTMessageV2();
request.setHttpMethod('GET');
request.setEndpoint(poll_request.endpoint_url);
request.setRequestHeader('Content-Type','application/json');
request.setBasicAuth(connection.getBasicAuthUser(), connection.getBasicAuthPassword());
// Save the response as a new attachment (on the poll request)
request.saveResponseBodyAsAttachment(
poll_request.getTableName(), poll_request.sys_id, params.attachment.file_name
);
response = request.execute();
poll_request.response_code = response.getStatusCode();
poll_request.response_status = response.getErrorMessage();
if (response.haveError()) {
throw '\nResponse Code: ' + response.getErrorCode() + '\nResponse error: ' + response.getErrorMessage();
}
// Return the sys_id of the saved attachment
answer = response.getResponseAttachmentSysid() + '';
})(poll_request, poller, connection, params);

Request script: This script uses the ServiceNow RESTMessageV2() web service to make a REST call to the endpoint url created in the Setup script. It returns the body of the request as an attachment who's sys_id it passes to the Response script

saveResponseBodyAsAttachment(): This method takes three parameters:

tableName - the table that contains the record you want to attach the saved file to.

recordSysId - the sys_id of the record you want to attach the saved file to.

fileName - the file name to give to the saved file.

request.execute(): The response object returned by request.execute() provides a method called getResponseAttachmentSysid().

getResponseAttachmentSysid(): This method returns the sys_id of the attachment generated by the REST call.

Your Request Script form should look like this:

7) Navigate to Response Script.

Response Script

The Response Script is used to process the information returned from the remote system. We will pass this data to Unifi, telling it which Message to use to process the data.

The last Poll Processor field to be configured is as follows:

#

Field

Description

Value

8

Response script

The script that processes the response to the request.

Update the code in the Response script field so that it looks like the code below

The code in the Response script field should look like this:

// Process the response returned by the request script
// The 'answer' variable from the request script is passed in here as the 'response' parameter
(function (poll_request, poller, response, params) {
// Establish the environment
var poll_helper = new x_snd_eb.PollHelper(poll_request);
var info = [
'Incident : ' + params.int_ref,
'- Attachment file name: ' + params.attachment.file_name,
'- Attachment id: ' + response
];
// The response should be the sys_id of the created attachment
params.attachment.data = '<x-attachment-data sys_id="' + response + '" />';
// Build the payload for Unifi
var payload = {
sys_id : params.ext_ref,
correlation_id : params.int_ref,
attachment : params.attachment
};
// Submit the message into Unifi
poll_helper.processInbound({
message_name : 'AddAttachmentInbound',
payload : JSON.stringify(payload)
});
poll_request.response_status = info.join('\n');
})(poll_request, poller, response, params);

Response script: This script sets up some objects to help us; this includes the essential PollHelper() function (which we initialise from the poll_request) along with the info [] array.

After that it sets params.attachment.data to the sys_id of the created attachment, setting up a payload object and submitting it to Unifi by calling the processInbound() method.

After processing the single result, it is logged to the Response status field of the Poll Request.

Essential code: the following lines of code must be included in the response script to enable Unifi

var poll_helper = new x_snd_eb.PollHelper(poll_request);
poll_helper.processInbound({
message_name : 'AddAttachmentInbound',
payload : JSON.stringify(payload)
});

Message name: Unifi needs to know the message name in order to know how to process the inbound request. We can either set it in the payload (as we did in previous Guides), or declare a variable that sets it which is passed into the processInbound() function along with the payload (as we have here). (Unifi will always check processInbound() first. If no Message name is set here it will use the 'Identify message script' on the Integration).

Your Response Script form should look like this:

9) Save the Poll Processor.

Now let's move on and configure the Get Attachment Poller.