// 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 constants
// 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 Attachments returned\n\n' + JSON.stringify(body,null,2);
"file_name": "MPFD-!--1539262705666--!",
"average_image_color": "",
"sys_updated_on": "2018-10-11 12:58:26",
"table_name": "incident",
"sys_id": "6c559892db812300f56c711ebf96192d",
"sys_updated_by": "ws_hn",
"download_link": "https://dev22291.service-now.com/api/now/attachment/6c559892db812300f56c711ebf96192d/file",
"content_type": "application/octet-stream",
"sys_created_on": "2018-10-11 12:58:26",
"size_compressed": "1055",
"table_sys_id": "bcdf18d5db241300f56c711ebf961947",
"sys_created_by": "ws_hn"
// Establish the environment
var poll_helper = new x_snd_eb.PollHelper(poll_request);
var integration = poller.getIntegration();
var conn = integration.getActiveConnection();
var cvar = conn.getVariables();
'Incident : ' + params.int_ref
// Use Unifi code to find the Bond for the Incident
var bond = new x_snd_eb.Bond(integration.getConfig());
bond.locateReference(integration,params.int_ref,params.ext_ref);
if ( !bond.isValidRecord() ) {
// If no bond was found, ignore this poll
info.push('- Bond not found - Attachments ignored');
poll_request.response_status = info.join('\n') + '\n\n' + JSON.stringify(body,null,2);
body.result.forEach(function(attach){
// Build an attachment object to pass on to the next poller
remote_id : attach.sys_id,
file_name : attach.file_name,
content_type : attach.content_type,
size_bytes : attach.size_bytes
info.push('- Get attachment (' + attach.remote_id + '): ' + attach.file_name);
x_snd_eb.Poller.execute( cvar.get_attachment_poller, {
int_ref : params.int_ref,
ext_ref : params.ext_ref,
// Keep track of the latest created_on time
attachment_hwm = attach.sys_created_on;
// Update Attachment High water mark (if later)
if ( attachment_hwm > params.attachment_hwm ) {
bond.setData(ATTACHMENT_HWM,attachment_hwm);
poll_request.response_status = info.join('\n') + '\n\n' + JSON.stringify(body,null,2);
})(poll_request, poller, response, params);