// 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) {
// DataStore name constant
// Storage of time based High Water Marker
var ATTACHMENT_HWM = 'attachment_hwm';
var body = JSON.parse(response);
// Nothing to do if no results were returned
if (body.result.length == 0) {
poll_request.response_status = 'No Incidents returned\n\n' + JSON.stringify(body, null, 2);
// Sample result (single Incident)
"sys_id": "0ecc4865db734010c3ebde82ca961960",
"correlation_id": "INC0010345",
"short_description": "Demo two - Fixing request",
"description": "A long description",
"sys_updated_on": "2020-04-02 14:00:00",
"sys_updated_by": "a.user"
// Establish the environment
var integration = poller.getIntegration();
var config = integration.getConfig();
var conn = integration.getActiveConnection();
var cvars = conn.getVariables();
var poll_helper = new x_snd_eb.PollHelper(poll_request);
// Use Unifi code to find the Bond for an Incident
var bond = new x_snd_eb.Bond(config);
bond.locateReference(integration, inc.correlation_id, inc.sys_id);
if (!bond.isValidRecord()) {
// Work out the message type to send to Unifi based upon the change of state
function get_message_name(curr, prev) {
// Default message type is an update
var message_name = 'UpdateIncidentInbound';
// Use the default type if we have no previous state
if (!prev || !prev.state) {
// Use the default type if there is no change in state
if (curr.state == prev.state) {
// We know the state has changed, check if it is Resolved (6)
message_name = 'ResolveIncidentInbound';
function execute_child_poller(inc, bond) {
// Set up a Poll to check for new attachments
x_snd_eb.Poller.execute(cvars.select_attachments_poller, {
int_ref: inc.correlation_id,
attachment_hwm: bond.getData(ATTACHMENT_HWM, '')
function process_incident(inc) {
// Log the incident number and time
var inc_time = inc.sys_updated_on;
info.push('Incident : ' + inc.number + ' (' + inc_time + ')');
// Find the bond on which the previous data is stored
var bond = get_bond(inc);
// If no bond was found, ignore this incident
info.push('- Bond not found - Incident ignored');
// Get the data stored for the incident by a previous poll
var previous_inc = bond.getDataObject('previous_inc', {
// Work out the message type to send into Unifi
var message_name = get_message_name(inc, previous_inc);
info.push('- Message name: ' + message_name);
// Set up the payload object for passing into Unifi
source_reference: inc.sys_id
short_description: inc.short_description,
description: inc.description,
close_code: inc.close_code,
close_notes: inc.close_notes
// Submit the message into Unifi
poll_helper.processInbound({
payload: JSON.stringify(payload)
execute_child_poller(inc, bond);
// Save the current incident as the previous incident for the next poll
bond.setDataObject('previous_inc', inc);
// Update last update time (if later)
var conn_time = conn.getData('last_update_time', '');
if (inc_time > conn_time) {
conn.setData('last_update_time', inc_time);
// Process the single result
process_incident(body.result);
poll_request.response_status = info.join('\n') + '\n\n' + JSON.stringify(body, null, 2);
})(poll_request, poller, response, params);