Urgent Help Needed: Jira Attachment Uploading as Corrupt File in ServiceNow

Andrea34
Tera Contributor
Good day, everyone. I hope you're all doing well.

 

I'm currently working on a scheduled job that downloads an attachment from Jira using a direct URL and then uploads it to a Change Request record in ServiceNow. However, I'm facing a critical issue:

ServiceNow automatically processes responses in binary format, and as a result, when the file is uploaded to the Change Request, it gets corrupted. The file either uploads as empty or, in the case of XLSX files, becomes unreadable.

I've tried countless solutions, but nothing has worked so far. I urgently need your help to resolve this.

Here is my code… Any guidance would be greatly appreciated.

 

 

(function executeScheduleJob() {
  try {
    var changeRequestSysId = "26ab7d4133e69a10ab0585434d5c7b73"; // Sys ID del Change Request en ServiceNow
    var attachmentUrl =
      "https://instance.atlassian.net/rest/api/2/attachment/content/167229"; // URL del archivo adjunto en Jira
    var attachmentName = "DVAN - TC no se visualiza en la app.xlsx";
    var attachmentMimeType =
      "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

    var jiraUrl = "https://banreservas-sandbox-149.atlassian.net";
    var email = "catiles@banreservas.com";
    var apiToken =
      "your_token";

    var auth = "Basic " + GlideStringUtil.base64Encode(email + ":" + apiToken);

    var attachmentData = new sn_ws.RESTMessageV2();
    attachmentData.setHttpMethod("GET");
    attachmentData.setEndpoint(attachmentUrl);
    attachmentData.setRequestHeader("Authorization", auth);

    var attachmentResponse = attachmentData.execute();
    var attachmentResponseBody = attachmentResponse.getBody();
	  var fileBytes = GlideStringUtil.base64DecodeAsBytes(attachmentResponseBody);
	  gs.info("FILE BYTES", fileBytes);
    var attachmentStatus = attachmentResponse.getStatusCode();

    if (attachmentStatus !== 200) {
      gs.error(
        "Error al descargar el adjunto de Jira. Código de estado: " +
          attachmentStatus
      );
      return;
    }

 

 
  } catch (error) {
    gs.error("Error en el Scheduled Job: " + error);
  }
})();