Send Attachment one instance to another
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2024 01:05 AM
Hi there I'm getting error as Script: Error parsing JSON response: Unexpected token:
Help me to fix it
(function executeRule(current, previous /*null when async*/) {
var targetInstanceURL = 'https://dev185360.service-now.com/';
var targetUserID = 'admin';
var password = ''; //Hide while posting question
var text = "";
var attachmentMsg = "";
try {
// Create REST Message to create a record
var r = new sn_ws.RESTMessageV2("E-Bonding dev185360", "Create Record");
r.setStringParameter("sys_id", current.sys_id);
r.setStringParameter("category", current.category);
r.setStringParameter("short_description", current.short_description);
r.setStringParameter("caller_id", current.caller_id);
var response = r.execute();
var responseBody = response.getBody();
var httpResponse = response.getStatusCode();
gs.log("HTTP Response: " + httpResponse);
gs.log("Response Body: " + responseBody);
if (httpResponse == 201 || httpResponse == 200) {
text = "Incident request processed successfully.";
var parsed;
try {
parsed = JSON.parse(responseBody);
} catch (jsonError) {
gs.error("Error parsing JSON response: " + jsonError.message);
text = "Incident creation failed. Invalid JSON response.";
gs.addErrorMessage(text);
return;
}
if (parsed.result && parsed.result.sys_id) {
var targetRecordSysId = parsed.result.sys_id;
text = "Incident Created Successfully. Sys ID: " + targetRecordSysId;
var attachmentCount = sendAttachments(current.getTableName(), current.sys_id, targetRecordSysId);
if (attachmentCount != 'none') {
attachmentMsg = " Attachments Successfully Sent: " + attachmentCount + " attachments.";
} else {
attachmentMsg = " No attachments found.";
}
} else {
text = "Incident creation response received, but sys_id is missing.";
gs.error(text);
}
} else {
text = "Incident not created. HTTP Response: " + httpResponse;
gs.error("Incident creation failed. Response Body: " + responseBody);
}
} catch (e) {
gs.error("Error in REST API call: " + e.message);
text = "Incident creation failed. Check error logs.";
}
text = text + " " + attachmentMsg;
gs.addInfoMessage(text);
// Function to send attachments
function sendAttachments(sourceTable, sourceID, targetID) {
var attachmentRecord = new GlideRecord("sys_attachment");
attachmentRecord.addQuery("table_sys_id", sourceID);
attachmentRecord.addQuery("table_name", sourceTable);
attachmentRecord.query();
var attachmentCount = 0;
try {
while (attachmentRecord.next()) {
gs.log("Processing attachment: " + attachmentRecord.file_name);
var aMsg = new sn_ws.RESTMessageV2(); // Fixed this
aMsg.setHttpMethod("post");
aMsg.setBasicAuth(targetUserID, password);
aMsg.setEndpoint(targetInstanceURL + 'api/now/attachment/file');
aMsg.setQueryParameter("table_name", sourceTable);
aMsg.setQueryParameter('table_sys_id', targetID);
aMsg.setQueryParameter("file_name", attachmentRecord.file_name);
aMsg.setQueryParameter("content_type", attachmentRecord.content_type);
aMsg.setRequestBodyFromAttachment(attachmentRecord.sys_id); // Set request body from attachment
var res = aMsg.execute();
var resBody = res.getBody();
var resStatus = res.getStatusCode();
gs.log("Attachment response status: " + resStatus);
gs.log("Attachment response body: " + resBody);
if (resStatus == 201) {
attachmentCount++; // Increment count if successful
} else {
gs.error("Failed to send attachment: " + resBody);
}
}
} catch (e) {
gs.error("Error while sending attachments: " + e.message);
}
return attachmentCount > 0 ? attachmentCount : 'none'; // Return count or 'none'
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2024 01:53 AM
Hi @nowitsvashu ,
do you've any special charecters in variables that you're setting ?
reffering this part:
var r = new sn_ws.RESTMessageV2("E-Bonding dev185360", "Create Record"); r.setStringParameter("sys_id", current.sys_id); r.setStringParameter("category", current.category); r.setStringParameter("short_description", current.short_description); r.setStringParameter("caller_id", current.caller_id);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2024 01:55 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2024 07:29 AM
not this,
The values that you're passing in sys_id, sd, cat,caller id.
do you have any special characters?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2024 08:36 AM
Did you check the logs for responseBody..at which step you are getting this error?