How to share the attachment to other ServiceNow instance record
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2024 11:35 PM
Hi All
We have an integration between two ServiceNow instance in which once incident is generated in one instance can log an incident to another instance. Now we want to share the attachment if any attached to instance to another instance. Below is the code so far we have to generate the incident. Can you please guide me how we can share the attachment-
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
gs.addInfoMessage("This is a update from create Rest message " );
try {
var r = new sn_ws.RESTMessageV2('XYZ_e-Bonding_incident', 'Create an Incident_XYZ');
var body = {
"short_description": current.getDisplayValue('short_description'),
"description": current.getDisplayValue('description'),
"impact": current.impact,
"urgency": current.urgency,
"correlation_id": current.getDisplayValue('sys_id'),
"correlation_display": current.getDisplayValue('number')
};
r.setRequestBody(JSON.stringify(body));
var response = r.execute();
var responseBody2 = JSON.parse(response.getBody());
var responseBody = JSON.stringify(responseBody2);
var httpStatus = response.getStatusCode();
gs.addInfoMessage("Response Body is : " + responseBody);
gs.addInfoMessage("httpStatus is : " + httpStatus);
if (httpStatus === 200 || httpStatus === 201) {
var ot = JSON.stringify(JSON.parse(responseBody))
var obj = JSON.parse(responseBody);
var foundSysId = obj.data.sys_id;
var foundDispId = obj.data.number;
current.correlation_id = foundSysId;
current.correlation_display = foundDispId;
current.u_third_party_reference = foundDispId;
current.update();
current.setWorkflow(false);
}
} catch (ex) {
var message = ex.message;
gs.addInfoMessage("message is :" + message);
}
})(current, previous);
Regards
Evan
1 REPLY 1
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2024 12:50 AM
Assuming your 'create attachment' is correctly configured, you could try something like this:
(function executeRule(current, previous /*null when async*/ ) {
gs.addInfoMessage("This is an update from create Rest message");
try {
var r = new sn_ws.RESTMessageV2('XYZ_e-Bonding_incident', 'Create an Incident_XYZ');
var body = {
"short_description": current.getDisplayValue('short_description'),
"description": current.getDisplayValue('description'),
"impact": current.impact,
"urgency": current.urgency,
"correlation_id": current.getDisplayValue('sys_id'),
"correlation_display": current.getDisplayValue('number')
};
r.setRequestBody(JSON.stringify(body));
var response = r.execute();
var responseBody2 = JSON.parse(response.getBody());
var responseBody = JSON.stringify(responseBody2);
var httpStatus = response.getStatusCode();
gs.addInfoMessage("Response Body is : " + responseBody);
gs.addInfoMessage("httpStatus is : " + httpStatus);
if (httpStatus === 200 || httpStatus === 201) {
var obj = JSON.parse(responseBody);
var foundSysId = obj.data.sys_id;
var foundDispId = obj.data.number;
current.correlation_id = foundSysId;
current.correlation_display = foundDispId;
current.u_third_party_reference = foundDispId;
current.update();
current.setWorkflow(false);
// Process attachments
var attachmentGr = new GlideRecord('sys_attachment');
attachmentGr.addQuery('table_sys_id', current.sys_id);
attachmentGr.query();
while (attachmentGr.next()) {
var attBody = new sn_ws.RESTMessageV2('XYZ_e-Bonding_incident', 'Create Attachment');
var attFileName = attachmentGr.file_name;
var attFileContent = new GlideSysAttachment().getBytes(attachmentGr);
attBody.setRequestHeader('Content-Type', 'application/octet-stream');
attBody.setRequestHeader('Content-Disposition', 'attachment; filename="' + attFileName + '"');
attBody.setRequestBody(attFileContent);
attBody.setStringParameterNoEscape('sys_id', foundSysId);
var attResponse = attBody.execute();
var attHttpStatus = attResponse.getStatusCode();
gs.addInfoMessage("Attachment Response Status: " + attHttpStatus);
if (attHttpStatus !== 200 && attHttpStatus !== 201) {
gs.addErrorMessage("Failed to send attachment: " + attFileName);
}
}
}
} catch (ex) {
var message = ex.message;
gs.addErrorMessage("Exception: " + message);
}
})(current, previous);
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark