- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2025 11:49 PM
How to Send attachment over E-Boding in ServiceNow.. I tried the below code but not working
var SendIncidentWithAttachments = Class.create();
SendIncidentWithAttachments.prototype = {
initialize: function() {},
sendIncident: function(incidentSysId) {
var inc = new GlideRecord('incident');
inc.get(incidentSysId);
var rest = new sn_ws.RESTMessageV2('Target ServiceNow', 'POST Incident');
rest.setHeader('ContentType', 'application/json');
var body = {
short_description: inc.short_description,
description: inc.description,
caller_id: inc.caller_id,
category: inc.category
};
rest.setStringParameterNoEscape('payload', body);
var response = rest.execute();
var result = response.getBody();
var targetSysId = result.sys_id;
this.sendAttachments(incidentSysId, targetSysId);
},
sendAttachments: function(srcSysId, destSysId) {
var attGR = new GlideRecord('sys_attachment');
attGR.addQuery('table_sys_id', srcSysId);
attGR.query();
while (attGR.next()) {
var attach = new GlideSysAttachment();
var data = attach.getContentBase64(attGR);
var fileName = attGR.file_name;
var upload = new sn_ws.RESTMessageV2('Target ServiceNow', 'POST Attachment');
upload.setRequestHeader("Content-Type", "application/json");
upload.setRequestBody(data);
upload.execute();
}
},
type: 'SendIncidentWithAttachments'
};
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2025 11:56 PM
@SandeepKSingh Your script sent the Base64 data directly using upload.setRequestBody(data). A standard REST API expects a structured JSON object, not just a raw string, especially when you need to provide metadata like the file name and the target record ID. and the script used result.sys_id, which is incorrect. The result of rest.execute() is a JSON string, and you need to parse it into an object to access its properties. The specific property holding the new record's sys_id depends on how the target's Scripted REST API is configured, but a common format is result.result.sys_id.
You can take reference of bellow script:
var SendIncidentWithAttachments = Class.create();
SendIncidentWithAttachments.prototype = {
initialize: function() {},
sendIncident: function(incidentSysId) {
var inc = new GlideRecord('incident');
if (inc.get(incidentSysId)) {
// Use the REST message defined in the REST Message form
var rest = new sn_ws.RESTMessageV2('Target ServiceNow', 'POST Incident');
rest.setHeader('Content-Type', 'application/json');
// Set the payload with stringified JSON
var body = {
short_description: inc.short_description,
description: inc.description,
caller_id: inc.caller_id,
category: inc.category
};
rest.setRequestBody(JSON.stringify(body));
var response = rest.execute();
var result = response.getBody();
// The target API must return the sys_id of the new record.
// You need to parse the response to get the sys_id.
var responseBody = JSON.parse(result);
var targetSysId = responseBody.result.sys_id;
if (targetSysId) {
this.sendAttachments(incidentSysId, targetSysId);
} else {
gs.error('Could not get target sys_id from REST response: ' + result);
}
}
},
sendAttachments: function(srcSysId, destSysId) {
var attGR = new GlideRecord('sys_attachment');
attGR.addQuery('table_sys_id', srcSysId);
attGR.query();
while (attGR.next()) {
var attach = new GlideSysAttachment();
var binData = attach.getBytes(attGR);
var base64Data = GlideStringUtil.base64Encode(binData);
var fileName = attGR.file_name;
var contentType = attGR.content_type;
// Use the REST message defined in the REST Message form
var upload = new sn_ws.RESTMessageV2('Target ServiceNow', 'POST Attachment');
upload.setRequestHeader("Content-Type", "application/json");
// Construct the JSON payload with necessary metadata
var uploadBody = {
'table_name': 'incident', // Target table name
'table_sys_id': destSysId, // Sys_id of the target record
'file_name': fileName,
'content_type': contentType,
'payload': base64Data
};
// Set the request body with the stringified JSON payload
upload.setRequestBody(JSON.stringify(uploadBody));
upload.execute();
}
},
type: 'SendIncidentWithAttachments'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2025 12:14 AM
This is the exact video you are looking for:
Check my video it wil help
https://www.youtube.com/watch?v=MQmBx8Rp4u8&t=5s&pp=ygUVYXR0YWNobWVudCBzZXJ2aWNlbm93
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2025 12:15 AM
Between Your script can be tweaked here below : Try the below out from my youtube video:
sendIncident: function(incidentSysId) {
var inc = new GlideRecord('incident');
if (!inc.get(incidentSysId)) {
gs.error('Incident not found');
return;
}
// Step 1: Send the main Incident details to target instance
var rest = new sn_ws.RESTMessageV2('Target ServiceNow', 'POST Incident');
rest.setRequestHeader('Content-Type', 'application/json');
var payload = {
short_description: inc.short_description + '',
description: inc.description + '',
caller_id: inc.caller_id.getDisplayValue() + '',
category: inc.category + ''
};
rest.setRequestBody(JSON.stringify(payload));
var response = rest.execute();
var responseBody = response.getBody();
var result = new global.JSON().decode(responseBody);
var targetSysId = result.result.sys_id;
gs.info('Incident created on target with sys_id: ' + targetSysId);
// Step 2: Send all attachments from source incident
this._sendAttachments(inc.sys_id, targetSysId);
return targetSysId;
},
_sendAttachments: function(sourceSysId, targetSysId) {
var att = new GlideRecord('sys_attachment');
att.addQuery('table_sys_id', sourceSysId);
att.query();
while (att.next()) {
var gsa = new GlideSysAttachment();
var bytes = gsa.getBytes(att);
var fileName = att.file_name + '';
// Prepare REST call for file upload
var upload = new sn_ws.RESTMessageV2('Target ServiceNow', 'POST Attachment');
upload.setRequestHeader("Content-Type", "application/octet-stream");
upload.setRequestHeader("Accept", "application/json");
upload.setRequestHeader("X-ATTACHMENT-NAME", fileName);
upload.setRequestHeader("X-TABLE-NAME", "incident");
upload.setRequestHeader("X-TABLE-SYS-ID", targetSysId);
upload.setRequestBody(bytes);
var resp = upload.execute();
gs.info('Attachment sent: ' + fileName + ' -> ' + resp.getStatusCode());
}
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2025 11:56 PM
@SandeepKSingh Your script sent the Base64 data directly using upload.setRequestBody(data). A standard REST API expects a structured JSON object, not just a raw string, especially when you need to provide metadata like the file name and the target record ID. and the script used result.sys_id, which is incorrect. The result of rest.execute() is a JSON string, and you need to parse it into an object to access its properties. The specific property holding the new record's sys_id depends on how the target's Scripted REST API is configured, but a common format is result.result.sys_id.
You can take reference of bellow script:
var SendIncidentWithAttachments = Class.create();
SendIncidentWithAttachments.prototype = {
initialize: function() {},
sendIncident: function(incidentSysId) {
var inc = new GlideRecord('incident');
if (inc.get(incidentSysId)) {
// Use the REST message defined in the REST Message form
var rest = new sn_ws.RESTMessageV2('Target ServiceNow', 'POST Incident');
rest.setHeader('Content-Type', 'application/json');
// Set the payload with stringified JSON
var body = {
short_description: inc.short_description,
description: inc.description,
caller_id: inc.caller_id,
category: inc.category
};
rest.setRequestBody(JSON.stringify(body));
var response = rest.execute();
var result = response.getBody();
// The target API must return the sys_id of the new record.
// You need to parse the response to get the sys_id.
var responseBody = JSON.parse(result);
var targetSysId = responseBody.result.sys_id;
if (targetSysId) {
this.sendAttachments(incidentSysId, targetSysId);
} else {
gs.error('Could not get target sys_id from REST response: ' + result);
}
}
},
sendAttachments: function(srcSysId, destSysId) {
var attGR = new GlideRecord('sys_attachment');
attGR.addQuery('table_sys_id', srcSysId);
attGR.query();
while (attGR.next()) {
var attach = new GlideSysAttachment();
var binData = attach.getBytes(attGR);
var base64Data = GlideStringUtil.base64Encode(binData);
var fileName = attGR.file_name;
var contentType = attGR.content_type;
// Use the REST message defined in the REST Message form
var upload = new sn_ws.RESTMessageV2('Target ServiceNow', 'POST Attachment');
upload.setRequestHeader("Content-Type", "application/json");
// Construct the JSON payload with necessary metadata
var uploadBody = {
'table_name': 'incident', // Target table name
'table_sys_id': destSysId, // Sys_id of the target record
'file_name': fileName,
'content_type': contentType,
'payload': base64Data
};
// Set the request body with the stringified JSON payload
upload.setRequestBody(JSON.stringify(uploadBody));
upload.execute();
}
},
type: 'SendIncidentWithAttachments'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2025 12:14 AM
This is the exact video you are looking for:
Check my video it wil help
https://www.youtube.com/watch?v=MQmBx8Rp4u8&t=5s&pp=ygUVYXR0YWNobWVudCBzZXJ2aWNlbm93
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2025 12:15 AM
Between Your script can be tweaked here below : Try the below out from my youtube video:
sendIncident: function(incidentSysId) {
var inc = new GlideRecord('incident');
if (!inc.get(incidentSysId)) {
gs.error('Incident not found');
return;
}
// Step 1: Send the main Incident details to target instance
var rest = new sn_ws.RESTMessageV2('Target ServiceNow', 'POST Incident');
rest.setRequestHeader('Content-Type', 'application/json');
var payload = {
short_description: inc.short_description + '',
description: inc.description + '',
caller_id: inc.caller_id.getDisplayValue() + '',
category: inc.category + ''
};
rest.setRequestBody(JSON.stringify(payload));
var response = rest.execute();
var responseBody = response.getBody();
var result = new global.JSON().decode(responseBody);
var targetSysId = result.result.sys_id;
gs.info('Incident created on target with sys_id: ' + targetSysId);
// Step 2: Send all attachments from source incident
this._sendAttachments(inc.sys_id, targetSysId);
return targetSysId;
},
_sendAttachments: function(sourceSysId, targetSysId) {
var att = new GlideRecord('sys_attachment');
att.addQuery('table_sys_id', sourceSysId);
att.query();
while (att.next()) {
var gsa = new GlideSysAttachment();
var bytes = gsa.getBytes(att);
var fileName = att.file_name + '';
// Prepare REST call for file upload
var upload = new sn_ws.RESTMessageV2('Target ServiceNow', 'POST Attachment');
upload.setRequestHeader("Content-Type", "application/octet-stream");
upload.setRequestHeader("Accept", "application/json");
upload.setRequestHeader("X-ATTACHMENT-NAME", fileName);
upload.setRequestHeader("X-TABLE-NAME", "incident");
upload.setRequestHeader("X-TABLE-SYS-ID", targetSysId);
upload.setRequestBody(bytes);
var resp = upload.execute();
gs.info('Attachment sent: ' + fileName + ' -> ' + resp.getStatusCode());
}
},