Search…
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.

Icons

Before continuing we would like to draw your attention to some of the relevant icons that are visible down the left hand navigation strip of the Unifi Integration Designer portal page.
The icons are:
a) 'Integration' icon: Opens the current integration's Details page.
b) 'Messages' icon: Opens the current integration's Messages page.
c) 'Fields' icon: Opens the current integration's Fields page.
d) 'Field Maps' icon: Opens the current integration's Field Maps page.
e) 'Pollers' icon: Opens the current integration's Pollers page.
f) 'Poll Processors' icon: Opens the current integration's Poll Processors page.

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>
Your New Poll Processor modal should look like this:
2) 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 Setup script field is to be configured as follows:
#
Field
Description
Value
3
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:
1
// Configure the new Poll Request record
2
(function(poll_request, poller, params) {
3
4
var gdt = new GlideDateTime();
5
gdt.addSeconds(-1800);
6
7
var connection = poller.getIntegration().getActiveConnection();
8
var user = connection.getBasicAuthUser();
9
var time = connection.getData('last_update_time', (gdt + ''));
10
11
var fields = 'sys_id,number,correlation_id,short_description,description,sys_updated_on,sys_updated_by';
12
13
var query = [
14
'correlation_idISNOTEMPTY',
15
'sys_updated_by!=' + user,
16
'sys_updated_on>' + time
17
];
18
19
var uri_query = query.map(function(c) {
20
return encodeURIComponent(c);
21
}).join('%5E');
22
23
poll_request.endpoint_url += '/table/incident?sysparm_query=' + uri_query +
24
'&sysparm_fields=' + encodeURIComponent(fields) + '&sysparm_limit=10';
25
26
})(poll_request, poller, params);
Copied!
Setup script: The parameters for which data to return are contained in the endpoint url.
fields: The table API will return all the data by default, so we choose to deliberately limit what is returned to the fields listed in this variable.
query: Instead of querying all the records on the remote instance, we deliberately limit the records to those where the correlation id is not empty (i.e. it's bonded), that haven't been updated by the Authentication user (our system) and that were updated since either since the last update time (if one exists), or in the last 30 minutes.
Endpoint URL: The value used in the poll_request.endpoint_url was initially generated using the ServiceNow REST API Explorer, substituting variables in for the uri query and fields. This value is appended to the existing endpoint url in the active connection before being added to the Poll Request.
Note: We have included the /table/incident element of the endpoint url because we used the truncated url in the Connection when following the Outbound Incident Guide. If you have used the full url there, then this element can be excluded here.
(We have also chosen to limit the number of records returned to 10.)
params: The params object is passed through to the subsequent scripts (and on to further Pollers, if required).
Your Setup Script form should look like this:
4) 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 Request script field is to be configured as follows:
#
Field
Description
Value
5
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:
1
// Process the request e.g. by executing a web service and returning the response
2
(function(poll_request, poller, connection, params) {
3
4
var rm = new sn_ws.RESTMessageV2();
5
rm.setEndpoint(poll_request.endpoint_url);
6
rm.setHttpMethod('GET');
7
rm.setRequestHeader('Accept', 'application/json');
8
rm.setRequestHeader('Content-type', 'application/json');
9
rm.setBasicAuth(connection.getBasicAuthUser(), connection.getBasicAuthPassword());
10
11
// rm.setRequestBody(JSON.stringify(body));
12
var resp = rm.execute();
13
14
answer = resp.getBody() + '';
15
16
})(poll_request, poller, connection, params);
Copied!
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 the answer which it passes to the Response script
Your Request Script form should look like this:
6) 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 to process.
The Response script field is to be configured as follows:
#
Field
Description
Value
7
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:
1
// Process the response returned by the request script
2
// The 'answer' variable from the request script is passed in here as the 'response' parameter
3
(function (poll_request, poller, response, params) {
4
5
var body = JSON.parse(response);
6
7
// Process the result
8
var conn = poller.getIntegration().getActiveConnection();
9
var cvars = conn.getVariables();
10
11
var poll_helper = new x_snd_eb.PollHelper(poll_request);
12
var info = [];
13
14
if ( body.result.length == 0 ) {
15
status = 'No Incident found';
16
poll_request.response_status = status + '\n\n' + JSON.stringify(body,null,2);
17
return;
18
}
19
20
body.result.forEach(function(inc){
21
22
// Set up the payload object
23
var pl = {
24
message : {
25
name: 'UpdateIncidentInbound',
26
source_reference: inc.sys_id
27
},
28
detail : {
29
short_description : inc.short_description,
30
description : inc.description
31
}
32
};
33
34
// Set up the options object for message submission
35
var opts = {
36
//message_name: 'UpdateIncidentInbound',
37
payload : JSON.stringify(pl)
38
};
39
40
poll_helper.processInbound(opts);
41
42
var inc_time = inc.sys_updated_on;
43
info.push('Incident : ' + inc.number + ' (' + inc_time + ')');
44
45
// Set last update time (if later)
46
var conn_time = conn.getData('last_update_time');
47
if ( inc_time > conn_time ) {
48
conn.setData('last_update_time',inc_time);
49
}
50
51
});
52
53
poll_request.response_status = info.join('\n') + '\n\n' + JSON.stringify(body,null,2);
54
55
})(poll_request, poller, response, params);
Copied!
Response script: This script parses the response into a body object to contain the result, (returning if it doesn't contain anything). It then sets up some objects to help us (including the essential PollHelper() function which we initialise from the poll_request). After that it loops through each of the returned tickets; for each it sets up a payload object and the options for submitting it to Unifi before calling the processInbound() method. It then checks whether the ticket was updated later than the last update time; if so, it sets and stores the last update time as that 'sys_updated_on' value (this 'last_update_time' is what the Setup script checks against when defining what the Poll will do).
After looping through all the records, the results are 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 send the data to Unifi
1
var poll_helper = new x_snd_eb.PollHelper(poll_request);
Copied!
1
poll_helper.processInbound(opts);
Copied!
processInbound(): Having the 'poll_helper.processInbound(opts);' inside the loop means that an update message will be generated for each updated ticket.
Payload object: We have chosen to structure the payload object (var pl) as we have. There is no obligation to keep the same structure. Yours can be defined to suit your requirements.
Payload options: In the payload options (var opts), we have commented out //message_name: 'UpdateIncidentInbound',. This is because we have chosen to include the message name in the structure of the payload object.
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 have), or in the options (as per the commented out line).
Your Response Script form should look like this:
8) Save the Poll Processor.
Now let's move on and configure the Poller.
Last modified 2mo ago