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);
  }
})();

 

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@Andrea34 

try this

(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);

        request.saveResponseBodyAsAttachment('change_request', changeRequestSysId, attachmentName); // added newly

        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);
    }
})();

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

@Andrea34 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Andrea34 

how does the response body look?

    var attachmentResponseBody = attachmentResponse.getBody();
gs.info(attachmentResponseBody);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@Andrea34 

try this

(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);

        request.saveResponseBodyAsAttachment('change_request', changeRequestSysId, attachmentName); // added newly

        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);
    }
})();

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Andrea34 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello,

 

I hope you're doing well.

The code is working perfectly! Even though I see messages in the logs like "

*** Script: Error en el Scheduled Job: com.glide.rest.util.RESTRuntimeException: Response body was requested to be saved as attachment. It's not available through getBody() anymore.: no thrown error

", the file is uploading without any issues and is not corrupted.

I have a question: Is it possible to do this with multiple issues? In other words, can multiple issues be retrieved for synchronization, then each issue is synced, and all attachments are uploaded?

Thank you very much!