Download Record attachments and related record attachments in a UI Action

ronaldorafael97
Tera Contributor

Hello dear community.
I am trying to configure a UI Action that allows me to download the attachments of my parent table and the attachments of the related elements, I have the following code, but when clicking on the action, only the attachments of the parent table are downloaded and not the attachments of related items.

 

function openURL() {
    var currentReqSysId = g_form.getUniqueValue();
    var relatedRecords = new GlideRecord('x_954779_table_secondary');
    relatedRecords.addQuery('parent', currentReqSysId);
    relatedRecords.query();

    while (relatedRecords.next()) {
        var attachments = new GlideRecord('sys_attachment');
        attachments.addQuery('table_name', relatedRecords.getTableName());
        attachments.addQuery('table_sys_id', relatedRecords.getUniqueValue());
        attachments.query();

        while (attachments.next()) {
            var attachmentSysId = attachments.getValue('sys_id');
            var attachmentName = attachments.getValue('file_name');

            var downloadURL = '/sys_attachment.do?sys_id=' + attachmentSysId;
            window.open(downloadURL, attachmentName); // Opens the attachment download URL in a new window/tab
        }
    }
}

 

 I am grateful for any help.

0 REPLIES 0