send attachement through rest message to different instance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-22-2025 11:17 PM
Hi All,
I am using ServiceNow rest message to copy incident record and attachment to a different instance. but the attachment not copying to target instance.
(function executeRule(current, previous /*null when async*/ ) {
try {
gs.log("1.0 " +current.sys_id);
var targetinstance = "https://dev211559.service-now.com/api/now/attachment";
var user = "userid";
var password = "password";
var text = '';
var r = new sn_ws.RESTMessageV2('ServiceNow e bonding', 'create an incident');
r.setStringParameterNoEscape("details", current.short_description);
r.setStringParameterNoEscape("caller", current.caller_id);
r.setStringParameterNoEscape("id", current.sys_id);
r.setStringParameterNoEscape("category", current.category);
r.setStringParameterNoEscape("number", current.number);
r.setStringParameterNoEscape("description", current.description);
r.setStringParameterNoEscape("assignment_group", current.assignment_group);
r.setStringParameterNoEscape("impact", current.impact);
r.setStringParameterNoEscape("urgency", current.urgency);
r.setStringParameterNoEscape("subcategory", current.subcategory);
var response = r.execute();
var responseBody = response.getBody();
gs.log(" 1.1 Fianl test integration response" + responseBody);
var parsedata = JSON.parse(responseBody);
var inctargetnum = parsedata.result.number;
current.correlation_id = inctargetnum;
//current.update();
var httpStatus = response.getStatusCode();
gs.log("2.0 get httpstatus value " + httpStatus);
if (httpStatus.toString() == "201") {
gs.log("2.1 check httpstatus conditon " + httpStatus);
text = "incident created";
var parser = new JSONParse();
var parsed = new parser.parse(responseBody);
var tragetrec = parsed.sys_id;
var attachmentcount = sendAttachements(current.getTableName(), current.sys_id, tragetrec);
// gs.log("3. print table name " + current.table_name);
}
} catch (ex) {
var message = ex.message;
}
function sendAttachements(sourcetable, sourceid, targetid) {
gs.log("4. attachment function call " + sourcetable + " " + sourceid + " " + targetid);
gs.sleep(2000);
var gr = new GlideRecord('sys_attachment');
gs.log("4.1 Querying attachments for table: " + sourcetable + ", sys_id: " + sourceid);
gr.addQuery('table_sys_id', sourceid);
gr.addQuery('table_name', sourcetable);
gr.query();
var count = 0;
while (gr.next()) {
count++;
gs.log("6. Attachment file name: " + gr.file_name);
gs.log("7. Attachment sys_id: " + gr.sys_id);
var ar = new sn_ws.RESTMessageV2();
ar.setHttpMethod("POST");
ar.setEndpoint(targetinstance);
ar.setBasicAuth(user, password);
ar.setRequestHeader("Accept", "application/json");
ar.setQueryParameter("table_name", "incident");
ar.setQueryParameter("table_sys_id", targetid);
ar.setQueryParameter("file_name", gr.file_name);
ar.setRequestBodyFromAttachment(gr.sys_id);
var respon = ar.execute();
gs.log("9. Attachment upload status: " + respon.getStatusCode());
gs.log("10. Attachment upload error: " + respon.getErrorMessage());
gs.log("11. Attachment upload response: " + respon.getBody());
}
gs.log("5. Attachment record count: " + count);
}
})(current, previous);
"sendAttachements" function not working .
Please let me know where I am making the mistake.
Thanks
Pradip
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2025 01:53 AM
Hi @G Ponsekar ,
i did try it by changing the url but the "sendAttachements" function not tiggering . see log screenshot.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2025 02:04 AM
Hi @pradippaul ,
Screenshot is not viewable. Not able to open your screenshot.
sendAttachements function is not triggering means you haven't received "201" status code for incident creation to enter the if statement. or you maybe wrongly passing the parameters for the function
Also, In sendAttachements function set Content-Type as well
If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!
Thanks, GP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2025 01:34 AM
Hi @pradippaul ,
is user name and password correct?
it seems what you set as user name = userid
and password = password
in the line number 7 & 8
aslo the target URL should be
https://dev211559.service-now.com/api/now/attachment/file
try making the URL change and also make changes to user name and password if they are wrong
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2025 01:38 AM
Yes, did use the correct password. I did not put it here but on the business rule i am using the correct password.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2025 01:39 AM
Did you get a chance to check my above response?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader