Attachments Uploading as Corrupt Files in Jira - Please help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2025 04:02 PM
Good afternoon, everyone. I hope you're all doing well.
I'm reaching out to urgently request your help with the following issue: I need to ensure that every attachment uploaded to a change request is successfully transferred to Jira. However, my code is currently sending corrupted files to Jira.
I've tried everything, but I can't figure out what’s causing this. Any guidance or suggestions would be greatly appreciated!
BUSINESS RULE
(function executeRule(current, previous /*null when async*/) {
gs.info("La Business Rule se está ejecutando. Adjunto: " + current.file_name);
if (current.table_name.toLowerCase() == "change_request") {
var changeTaskGR = new GlideRecord("change_request");
if (changeTaskGR.get(current.table_sys_id)) {
var jiraIssueId = changeTaskGR.getValue("u_id_hu");
if (jiraIssueId) {
gs.info("Jira Issue ID encontrado: " + jiraIssueId);
// Enviar solo adjuntos nuevos
sendNewAttachmentsToJira(
current.table_sys_id,
jiraIssueId,
"change_request",
current.sys_created_on
);
} else {
gs.warn(
"El campo u_nreferens no tiene valor en la change_task con sys_id: " +
current.table_sys_id
);
}
} else {
gs.warn(
"No se encontró un registro change_task con sys_id: " +
current.table_sys_id
);
}
}
})(current, previous);
function sendNewAttachmentsToJira(
sysId,
jiraIssueId,
tableName,
currentCreatedOn
) {
var attachmentGR = new GlideRecord("sys_attachment");
attachmentGR.addQuery("table_name", tableName);
attachmentGR.addQuery("table_sys_id", sysId);
attachmentGR.addQuery("sys_created_on", ">=", currentCreatedOn); // Solo adjuntos nuevos
attachmentGR.query();
while (attachmentGR.next()) {
var attachmentContent = new GlideSysAttachment().getBytes(attachmentGR);
var boundary = "----WebKitFormBoundary" + gs.generateGUID();
var fileName = attachmentGR.file_name;
var contentType = attachmentGR.content_type;
var requestBody =
"--" +
boundary +
"\r\n" +
'Content-Disposition: form-data; name="file"; filename="' +
fileName +
'"\r\n' +
"Content-Type: " +
contentType +
"\r\n\r\n" +
attachmentContent +
"\r\n" +
"--" +
boundary +
"--";
var attachmentRequest = new sn_ws.RESTMessageV2();
attachmentRequest.setHttpMethod("POST");
attachmentRequest.setEndpoint(
"https://banreservas-sandbox-149.atlassian.net/rest/api/3/issue/" +
jiraIssueId +
"/attachments"
);
var base64Auth = GlideStringUtil.base64Encode(
"user" +
":" +
"token"
);
attachmentRequest.setRequestHeader("Authorization", "Basic " + base64Auth);
attachmentRequest.setRequestHeader("X-Atlassian-Token", "no-check");
attachmentRequest.setRequestHeader(
"Content-Type",
"multipart/form-data; boundary=" + boundary
);
attachmentRequest.setRequestBody(requestBody);
try {
var attachmentResponse = attachmentRequest.execute();
var status = attachmentResponse.getStatusCode();
var responseBody = attachmentResponse.getBody();
if (status == 200 || status == 201) {
gs.info(
'Attachment "' +
fileName +
'" subido exitosamente a Jira issue ID: ' +
jiraIssueId
);
} else {
gs.warn(
"No se pudo subir el attachment. Status: " +
status +
", Respuesta: " +
responseBody
);
}
} catch (ex) {
gs.error("Error al subir el attachment a Jira: " + ex.message);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2025 06:41 PM
Hi, when debugging the business rule, are you finding which line the break is occurring on? Out of curiosity, do you have any other similar functions that currently execute successfully, or is this your first time attempting it?
If I've at all helped, please return the favor by clicking the thumb next to my post. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2025 08:51 PM
did this work in the past or this is a fresh implementation?
I believe you need to send base64 encoded data and not bytes for attachment. add these 2 lines and try to send the file
Also encouraging to add some gs.info() for debugging purpose
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-14-2025 05:41 AM
Hello, I hope you're doing well.
In this case, it didn't work—the file is still being sent with more weight than expected. For example, if I upload a file of 128 bytes, Jira processes it and uploads it as 172 bytes.
Any idea why this might be happening?
Best regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-14-2025 05:51 AM
did they confirm the file is actually received correctly?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader