Download attachment by passing file_name instead of sys_id

Ovin Detree
Kilo Explorer

We are able to download an attachment using the sys id by using the below URL in the web browser or postman.

https://servicenow-instance.ourdomain.com/sys_attachment.do?sys_id=21e63dce1fbec118fb5252c7b04cbcd4

Can we download an attachment by querying just with the filename?

I tried: https://servicenow-instance.ourdomain.com/sys_attachment.do?file_name=myfile.xls but it didn't work.

Thanks in advance.

 

15 REPLIES 15

Hitoshi Ozawa
Giga Sage
Giga Sage

Following is an example Scripted API to download file attached to an incident.

Query Parameters are "number" (incident number) and "filename" (name of attached file).

Scripted API

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var tableName = 'incident';

    var queryParams = request.queryParams;
    var number = queryParams.number.toString();
    var filename = queryParams.filename.toString();

    try {
        var grTable = new GlideRecord(tableName);
        if (grTable.get('number', number)) {
            var grAttach = new GlideRecord('sys_attachment');
            grAttach.addQuery('table_sys_id', grTable.sys_id);
            if (filename && filename.length > 0) {
                grAttach.addQuery('file_name', filename);
            }
            grAttach.query();
            if (grAttach.next()) {
                filename = grAttach.file_name;
                var content_type = grAttach.content_type;
                var created = grAttach.created;
                var created_by = grAttach.created_by;

                var file_ext = content_type.substr(content_type.lastIndexOf('/') + 1);
                var hdrs = {},
                    attachment_sys_id = filename + file_ext;
                hdrs['Content-Type'] = content_type;

                response.setStatus(200);
                response.setHeaders(hdrs);

                var writer = response.getStreamWriter();
                var attachmentStream = new GlideSysAttachmentInputStream(grAttach.sys_id);
                writer.writeStream(attachmentStream);
            }
        }
    } catch (e) {
        gs.error("ERROR=", e);
    }


})(request, response);

URL example:

https://<instance name>.service-now.com/api/<scripted API ID>/download_attachment?number=INC0000015&filename=hello_message.png

I am trying to have a direct URL to the file using the filename because we have a process in Java which needs to access the data in this excel file. I understand the sys_id will not be same everytime a new version is uploaded but we know that the filename will be same so we are trying to fetch it using the file name.

Thanks in advance.

Ovin

There's 2 choices, write a route in Java to call the ServiceNow APIs to find the sys_attachment sys_id or to write a Scripted REST API in ServiceNow. With Scripted REST API, Java app will only have to call once.

My only option is "write a route in Java to call the ServiceNow APIs to find the sys_attachment sys_id"

Is there a documentation or a sample code to find how to do this?

I'm in an emergency situation right now. I'll try to get my eclipse up when I get some time. Would need to call ServiceNow several times to get the file.