Need Help to Create a UI Action Link to download the File

Manasa23
Tera Contributor

HI,

I have a Requirement were I need to create UI ACTION link on the form.

If the user click on the link, The template should be downloaded.

I have stored the template in the Attachment table.

How to Achieve this ?


Thanks,
M  

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can make the UI action as client side

On click - downloadRecord()

Script

function downloadRecord(){
	var sysId = ''; // give the sys_id of the attachment record
	var url = '/sys_attachment.do?sys_id=' + sysId;
	window.open(url);
}

Regards
Ankur

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

Hi,

you can make the UI action as client side

On click - downloadRecord()

Script

function downloadRecord(){
	var sysId = ''; // give the sys_id of the attachment record
	var url = '/sys_attachment.do?sys_id=' + sysId;
	window.open(url);
}

Regards
Ankur

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

HI Ankur,

Thanks for the Script. It works fine when i hardcode the SYSID.

I have stored the SYS ID of the Attachment Record in System property..

This Script is not working When i try to call the system property using gs.getProperty()..

can you help !!



Thanks,
M

Hi,

you cannot get system property using gs object in client side.

you can use display business rule on that table and store that sys_id in g_scratchpad

then use it in your UI action

Display BR:

g_scratchpad.sysId = gs.getProperty('propertyName'); // your property name

UI Action Script:

function downloadRecord(){
	var sysId = g_scratchpad.sysId;
	var url = '/sys_attachment.do?sys_id=' + sysId;
	window.open(url);
}

regards
Ankur

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

eumak
Tera Guru

Hello Manasa,

you can use one of the best method to downloads all the files at a time in zip folder. 

1) Create a UI Action on the table 
Name:Save Attachments as ZIP
Condition - current.hasAttachments();
Script - 

action.setRedirectURL('exportAttachmentsToZip.do?sysparm_sys_id=' + current.sys_id + '&sysparm_table=' + current.getTableName());

2) Create a processor 

 System Definition->Processors

Name: exportAttachmentsToZip
Type: script
Path: exportAttachmentsToZip
Script:

var sysid = g_request.getParameter('sysparm_sys_id');
var table = g_request.getParameter('sysparm_table');

var theRecord = new GlideRecord(table);
theRecord.addQuery('sys_id', sysid);
theRecord.query();
theRecord.next();

var zipName = 'attachments.zip';

var StringUtil = GlideStringUtil;

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', theRecord.sys_id);
gr.addQuery('table_name', theRecord.getTableName());
gr.query();

if (gr.hasNext()){
    g_response.setHeader('Pragma', 'public');
    g_response.addHeader('Cache-Control', 'max-age=0');
    g_response.setContentType('application/octet-stream');
    g_response.addHeader('Content-Disposition', 'attachment;filename=' + zipName);
    var out = new Packages.java.util.zip.ZipOutputStream(g_response.getOutputStream());
    var count=0;
    while (gr.next()){
        var sa = new GlideSysAttachment();
        var binData = sa.getBytes(gr);
       
        var file = gr.file_name;
        addBytesToZip(out, zipName, file, binData);
        count ++;
    }
    // Complete the ZIP file
    out.close();
}

function addBytesToZip (out, dir, file, stream){
    // Add ZIP entry to output stream.
    out.putNextEntry(new Packages.java.util.zip.ZipEntry(file));
    out.write(stream, 0, stream.length);
    out.closeEntry();
}

 

The above solution has been referred by SNGURU.  I have implemented it on my PDI & it worked well.

Mark useful & Correct , if it helped you.

Cheers..!
Happy Learning
Tushar 

Mark it as helpful or correct, If Applicable


Cheers..!

Happy Learning:)

Tushar