Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to export attachments of multiple records at once

Jessica1307
Tera Expert

I want to download some attachement of multiple records at once, Is this possible?

1 ACCEPTED SOLUTION

try this

function downloadAll() {
    var selectedSysIds = g_list.getChecked();
    var arr = selectedSysIds.split(',');
    for (var i = 0; i < arr.length; i++) {
        alert(arr[i]);
        var gr = new GlideRecord("sys_attachment");
        gr.addQuery("table_sys_id", arr[i]);
        gr.query();
        if (gr.next()) {
            alert(123);
            var url = '/sys_attachment.do?sys_id=' + gr.sys_id;
            g_navigation.open(url, '_blank');            
        }
    }
}

Regards
Ankur

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

View solution in original post

19 REPLIES 19

Hi, Ankur Bawiskar

I click this action but nothing happened. I am sure I selected records which have attachment

find_real_file.png

Hello can you double check the query in the sys_attachment table to see if you are getting records in the list view and also try putting in logs/alerts to see if you are able to get into the function

It only can print one record sysid and123 can show , It seems window.open('/sys_attachment.do?sys_id=' + gr.sys_id, "_blank"); not worked

function downloadAll() {
    var selectedSysIds = g_list.getChecked();
    var arr = selectedSysIds.split(',');
    for (var i = 0; i < arr.length; i++) {
		alert(arr[i]);
        var gr = new GlideRecord("sys_attachment");
        gr.addQuery("table_sys_id", arr[i]);
        gr.query();
        if (gr.next()) {
			alert(123);
            window.open('/sys_attachment.do?sys_id=' + gr.sys_id, "_blank");
        }
    }
}

Hi,

update as this

function downloadAll() {
    var selectedSysIds = g_list.getChecked();
    var arr = selectedSysIds.split(',');
    for (var i = 0; i < arr.length; i++) {
        alert(arr[i]);
        var gr = new GlideRecord("sys_attachment");
        gr.addQuery("table_sys_id", arr[i]);
        gr.query();
        if (gr.next()) {
            alert(123);
            var url = '/sys_attachment.do?sys_id=' + gr.sys_id;
            g_navigation.open(url, '_blank');            
        }
    }
}

Regards
Ankur

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

Hi , I tried your new code, when I selected three records ,and alert can show three records' sysid, But it only download an attachment of one record, why? thank you!

 

function downloadAll() {
    var selectedSysIds = g_list.getChecked();
    var arr = selectedSysIds.split(',');
    alert(arr);
    for (var i = 0; i < arr.length; i++) {
        var gr = new GlideRecord("sys_attachment");
        gr.addQuery("table_sys_id", arr[i]);
        gr.query();
        if (gr.next()) {
            var url = '/sys_attachment.do?sys_id=' + gr.sys_id;
            g_navigation.open(url, '_blank');
        }
    }
}