- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2022 01:53 AM
I want to send attachment from ServiceNow to Jira. For that I have created REST message and BR. I don't know what is wrong with my BR. I am not able to send attachment
(function executeRule(current, previous /*null when async*/ ) {
var included_tables = ["incident"];
var debug = true;
var arrayUtil = new ArrayUtil();
var requestBody;
var responseBody;
var status;
var sm;
if (typeof GlideStringUtil != 'undefined')
var StringUtil = GlideStringUtil;
else
var StringUtil = Packages.com.glide.util.StringUtil;
gs.log("initialize");
var gr = new GlideRecord(current.table_name);
gr.addQuery('sys_id', current.table_sys_id);
gr.query();
if (gr.next()) {
if (arrayUtil.contains(included_tables,current.table_name)) {
try{
sm = new sn_ws.RESTMessageV2('Neste Siili JIRA', 'Add attachment');
var attachmentsJson = '';
attachmentsJson += ' { "content_type":"' + current.content_type + '",';
attachmentsJson += '"file_name":"' + JSUtil.escapeText(current.file_name) + '",';
attachmentsJson += '"size_bytes":"' + current.size_bytes + '",';
attachmentsJson += '"sys_id":"' + current.sys_id + '"';
attachmentsJson += '}';
sm.setStringParameterNoEscape('attachments', attachmentsJson);
response = sm.execute();
status = response.getStatusCode();
if (debug){
gs.log("Payload sent jira: " + sm.getRequestBody());
gs.log("Response jira: " + response.getBody());
gs.log("Status jira : " + status);
}
} catch(ex) {
responseBody = ex.getMessage();
status = '500';
} finally {
requestBody = sm ? sm.getRequestBody():null;
}
}
}
})(current, previous);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2022 10:43 PM
Hi,
I am sharing a sample code and you can enhance it further
based on BR is on which table sys_attachment or incident enhance the code
var arr = [];
var gsu = (typeof GlideStringUtil != 'undefined') ? (GlideStringUtil) : (Packages.com.glide.util.StringUtil); //few versions support the first one, other supports second
var gsa = (typeof GlideSysAttachment != 'undefined') ? (new GlideSysAttachment()) : (new Packages.com.glide.ui.SysAttachment());
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_sys_id", incidentSysId);
gr.query();
while (gr.next()) {
var attachmentData = gsa.getBytes(gr);
var attachment = String(Packages.java.lang.String(attachmentData));
var encData = GlideStringUtil.base64Encode(attachmentData);
var obj = {};
obj["content_type"] = gr.getValue('content_type');
obj["file_name"] = JSUtil.escapeText(gr.getValue('file_name'));
obj["size_bytes"] = gr.getValue("size_bytes ");
obj["sys_id"] = gr.getUniqueValue();
obj["Content-Transfer-Encoding"] = "base64" + encData.toString();
arr.push(obj);
}
var finalObj = {};
finalObj["attachment"] = arr;
var sm = new sn_ws.RESTMessageV2('Neste Siili JIRA', 'Add attachment');
var response = sm.execute();
var status = response.getStatusCode();
gs.info("Payload sent jira: " + sm.getRequestBody());
gs.info("Response jira: " + response.getBody());
gs.info("Status jira : " + status);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2022 02:11 AM
Hi Shalika,
Hope you are doing fine.
For the above use case what you can do is to first convert the file into base 64 string and then send it over the API which can be reiterate to file on the JIRA side.
Please find the reference code for the same.
var target = new GlideRecord('sys_attachment');
target.addQuery('table_name', 'incident');
target.addQuery('table_sys_id', current.sys_id);
target.query();
while(target.next()) {
var sa = new GlideSysAttachment();
var binData = sa.getBytes(target);
var base64Data = GlideStringUtil.base64Encode(binData);
//Send Attachments
var requestAttachment = new sn_ws.RESTMessageV2();
requestAttachment.setEndpoint('https://XXXXX.service-now.com/api/now/import/u_attachment_staging_table');
requestAttachment.setHttpMethod('POST');
requestAttachment.setBasicAuth(user,password);
requestAttachment.setRequestHeader("Accept","application/json");
requestAttachment.setRequestHeader('Content-Type','application/json');
var requestAttachmentJSONBody = createRequestBody("u_name", target.file_name + ":" + target.content_type);
requestAttachmentJSONBody += addToRequestBody("u_ticket_number", 'incident:' + sysid);
requestAttachmentJSONBody += addToRequestBody("u_attachment", base64Data);
requestAttachmentJSONBody += closeRequestBody();
requestAttachment.setRequestBody(requestAttachmentJSONBody);
var responseAttachment = requestAttachment.execute();
}
Please mark the answer correct if its of any help .
Regards,
Amit Gujarathi
(Technomonk)
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2022 05:25 AM
Nothing is happening.
Also, in
requestAttachment.setBasicAuth(user,password);
Here, I should write the username and password?
In
requestAttachment.setEndpoint('https://XXXXX.service-now.com/api/now/import/u_attachment_staging_table');
What is u_attachment_staging_table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2022 12:00 AM
Hii,
I tried using this BR, but the attachments is not being added to Jira.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2022 02:21 AM
Hi,
Are you sure you are sending the JSON request body in the exact same manner as expected by JIRA?
What's the http status and error message?
regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader