Search…
Edit Child Poll Processor
Our previously configured Child Poll Processor only polled for updates against records passed to it from its Parent. We will now edit it so that it will also kick off the Select Attachments Poller.
Rather than just making edits to the existing Child Poll Processor, we will instead copy it & edit the copy. That way we will have one Child Poll Processor that just polls for updates and another that polls for updates and attachments.

Copy Child Poll Processor

To copy the Child Poll processor, in Unifi Integration Designer, click the 'Poll Processors' Icon.
Click the ellipsis to the right of < Your Child Poll Processor > (created following the Incident Parent and Child Poller Guide) & then click Copy.

Copy Poll Processor Modal

Click Copy.
Your Copy Poll Processor modal should look like this:
You will be redirected to the Details page of the newly copied Poll Processor.

Edit Copied Poll Processor

The fields to edit for the Copied Poll Processor are as follows:
Field
Description
Value
Name
The name of the Processor.
<Your Name>*
*Name: We've chosen to append the existing value with, "(with Attachments)".
Your Copied Poll Processor should look like this:
Navigate to Scripts > Response Script.

Response Script

The Response Script field is to be edited as follows:
Field
Description
Value
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
// DataStore name constant
6
// Storage of time based High Water Marker
7
var ATTACHMENT_HWM = 'attachment_hwm';
8
9
var body = JSON.parse(response);
10
11
// Nothing to do if no results were returned
12
if (body.result.length == 0) {
13
poll_request.response_status = 'No Incidents returned\n\n' + JSON.stringify(body, null, 2);
14
return;
15
}
16
17
// Sample result (single Incident)
18
/*
19
{
20
"result": {
21
"sys_id": "0ecc4865db734010c3ebde82ca961960",
22
"number": "INC0010107",
23
"correlation_id": "INC0010345",
24
"short_description": "Demo two - Fixing request",
25
"description": "A long description",
26
"state":"2",
27
"sys_updated_on": "2020-04-02 14:00:00",
28
"sys_updated_by": "a.user"
29
}
30
}
31
*/
32
33
// Establish the environment
34
var integration = poller.getIntegration();
35
var config = integration.getConfig();
36
var conn = integration.getActiveConnection();
37
var cvars = conn.getVariables();
38
39
var poll_helper = new x_snd_eb.PollHelper(poll_request);
40
var info = [];
41
42
// Use Unifi code to find the Bond for an Incident
43
function get_bond(inc) {
44
var bond = new x_snd_eb.Bond(config);
45
bond.locateReference(integration, inc.correlation_id, inc.sys_id);
46
if (!bond.isValidRecord()) {
47
return null;
48
}
49
return bond;
50
}
51
52
// Work out the message type to send to Unifi based upon the change of state
53
function get_message_name(curr, prev) {
54
55
// Default message type is an update
56
var message_name = 'UpdateIncidentInbound';
57
58
// Use the default type if we have no previous state
59
if (!prev || !prev.state) {
60
return message_name;
61
}
62
63
// Use the default type if there is no change in state
64
if (curr.state == prev.state) {
65
return message_name;
66
}
67
68
// We know the state has changed, check if it is Resolved (6)
69
if (curr.state == '6') {
70
message_name = 'ResolveIncidentInbound';
71
}
72
73
return message_name;
74
}
75
76
function execute_child_poller(inc, bond) {
77
78
if (!bond) {
79
return {};
80
}
81
82
// Set up a Poll to check for new attachments
83
x_snd_eb.Poller.execute(cvars.select_attachments_poller, {
84
int_ref: inc.correlation_id,
85
ext_ref: inc.sys_id,
86
attachment_hwm: bond.getData(ATTACHMENT_HWM, '')
87
});
88
89
}
90
91
function process_incident(inc) {
92
93
// Log the incident number and time
94
var inc_time = inc.sys_updated_on;
95
info.push('Incident : ' + inc.number + ' (' + inc_time + ')');
96
97
// Find the bond on which the previous data is stored
98
var bond = get_bond(inc);
99
100
// If no bond was found, ignore this incident
101
if (!bond) {
102
info.push('- Bond not found - Incident ignored');
103
return;
104
}
105
106
// Get the data stored for the incident by a previous poll
107
var previous_inc = bond.getDataObject('previous_inc', {
108
state: '2'
109
});
110
111
// Work out the message type to send into Unifi
112
var message_name = get_message_name(inc, previous_inc);
113
info.push('- Message name: ' + message_name);
114
115
// Set up the payload object for passing into Unifi
116
var payload = {
117
message: {
118
name: message_name,
119
source_reference: inc.sys_id
120
},
121
detail: {
122
short_description: inc.short_description,
123
description: inc.description,
124
state: inc.state,
125
close_code: inc.close_code,
126
close_notes: inc.close_notes
127
}
128
};
129
130
// Submit the message into Unifi
131
poll_helper.processInbound({
132
payload: JSON.stringify(payload)
133
});
134
135
// Check for Attachments
136
execute_child_poller(inc, bond);
137
138
// Save the current incident as the previous incident for the next poll
139
bond.setDataObject('previous_inc', inc);
140
141
// Update last update time (if later)
142
var conn_time = conn.getData('last_update_time', '');
143
if (inc_time > conn_time) {
144
conn.setData('last_update_time', inc_time);
145
}
146
147
}
148
149
// Process the single result
150
process_incident(body.result);
151
152
poll_request.response_status = info.join('\n') + '\n\n' + JSON.stringify(body, null, 2);
153
154
})(poll_request, poller, response, params);
Copied!
Response script: This script creates a variable to store a time based high water marker and parses the response into a body object to contain the result, (returning if it doesn't contain anything).
It then establishes the environment, getting the Integration, Configuration, Connection & Connection Variables and sets up the essential PollHelper() function (which we initialise from the poll_request) along with the info [] array.
After that it sets up some other functions: get_bond() finds the bond for the Incident (returning if there isn't one), get_message_name() works out the message type to send to Unifi based upon the change of state & execute_child_poller() sets up a Poll Request to check for new attachments.
It then processes each of the results (passed in tickets). For each, it logs the incident number & time, finding the bond & returning any previous data stored on the bond, deciding which Message to use, setting up a payload object and submitting it to Unifi by calling the processInbound() method. It then runs the execute_child_poller() function and saves the current incident as the previous incident for the next poll & 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).
On processing each result, it is then logged to the Response status field of the Poll Request.
params: The params object is passed through to the subsequent scripts (and on to further Pollers, if required). This is used to pass the int_ref, ext_ref & attachment_hwm elements to the child Poller.
Code Edits - The following code snippets were added to the existing codebase to facilitate polling for attachments:
1
var ATTACHMENT_HWM = 'attachment_hwm';
Copied!
This is a DataStore name constant to store a time based high water mark.
1
function execute_child_poller(inc, bond) {
2
3
if (!bond) {
4
return {};
5
}
6
7
// Set up a Poll to check for new attachments
8
x_snd_eb.Poller.execute(cvars.select_attachments_poller, {
9
int_ref: inc.correlation_id,
10
ext_ref: inc.sys_id,
11
attachment_hwm: bond.getData(ATTACHMENT_HWM, '')
12
});
13
14
}
Copied!
The x_snd_eb.Poller.execute() method has two parameters. In the first we pass the sys_id of the child Poller (as created on the 'Select Attachments Poller' page). In the second we pass an object containing the correlation_id on the bonded Incident as the int_ref element, the sys_id of the bonded Incident as the ext_ref element and the value of the ATTACHMENT_HWM DataStore as the attachment_hwm element (telling the child Poller which attachment records to poll).
1
execute_child_poller(inc, bond);
Copied!
Causing the execute_child_poller() function to run (as set up above) - passing in the Incident & Bond.
Your Response Script form should look like this:
Click Save.

Update Select Attachments Poll Processor

Navigate to and open the Select Attachments Poll Processor (created on the 'Select Attachments Poll Processor' page) and update the value of the Parent field by selecting its parent Poll Processor created above.
Remember, We have not scripted any business logic to run from the value in the Parent field. It is given for documentary purposes only (for you to easily identify which parent Poll Processor kicked off the child).
The following Poll Processors should now be in place on the Integration:
We could Deactivate the copied "Unifi - SN REST Poller Single Incident" now that we've created this version, "with Attachments". However, we will instead edit the Child Update Poller that runs it.
Now let's move on and edit the Child Update Poller (configured when following the Incident Parent and Child Poller Guide) so that it runs the Poll Processor created above.