logo

NJP

Downloading excel file on Browser

Import · Dec 17, 2019 · article

I have struggled a lot to find out the solution for extracting the CSV/excel file of desired results on click on UI action.

Kindly,note this involves DOM hence,we are triggering UI page from UI action and downloadin the results with desired headers.

Points to be noted:

1.No use of URL like mentioned in the thread

https://docs.servicenow.com/bundle/newyork-platform-administration/page/administer/exporting-data/ta...

2. Not attaching the file on the form rather downloading it under the browser downloads

3. Can we used for fetching related data from other tables also like problem,incident etc from change form

Let us take a case where on click of an UI action 'FETCH REPORT' on change form,a glidedialog window should appear to select the incident record(default is the current record) with OK & Cancel buttons.

If pressed on OK then all the related change tasks would get attached to the rows of sheet with predefined headers.

Note:You can include other table records like problems,incidents etc into same report also.

UI Action:

Name- Fetch Report,Client - true,action name-ftc_chg,form button- true

Script-

function openInput(){

var dialog = new GlideDialogWindow('change_report');

dialog.setSize(750,300);dialog.setTitle('Select the change number to generate report..');dialog.setPreference("target", g_form.getTableName()); dialog.setPreference("target_id", g_form.getUniqueValue());dialog.setPreference("target_disp", g_form.getValue('number')); dialog.render();

}

UI page:

Name-change_report

HTML-

<?xml version="1.0" encoding="utf-8" ?>

Release

var val = RP.getWindowProperties().get('target_id'); val;/g:evaluate var disp = RP.getWindowProperties().get('target_disp'); disp; /g:evaluate /g:ui_form

/j:jelly

Client side:

var resId = gel("chg_id").value;

function triggerEmail(){

if(resId!==""){ // Headers to be included in the CSV var Head = [[ 'Number', 'Short Description', 'State', 'Assigned To' ]]; var row =[]; // create the arrow for the list values answers var gr = new GlideRecord('task'); gr.addQuery('parent',resId); // filtering out the created records from table gr.query(); while(gr.next()){ row.push({key1:gr.number,key2:gr.short_description, key3:gr.state, key4:gr.assigned_to});

}

// insert the values under the proper headers for (var l = 0; l < row.length; ++l) { Head.push([ row[l].key1, row[l].key2, row[l].key3, row[l].key4 ]);

}

var csvRows = [];for (var cell = 0; cell < Head.length; ++cell) { csvRows.push(Head[cell].join(','));

}

// function to download the excel var csvString = csvRows.join("\n");var csvFile = new Blob([csvString], { type: "text/csv" });var downloadLink = document.createElement("a");downloadLink.download = 'Change Report.csv';downloadLink.href = window.URL.createObjectURL(csvFile);downloadLink.style.display = "none";document.body.appendChild(downloadLink);downloadLink.click();

}

else{alert("Kindly select the Release in the field as it can't be empty"); }

}

Kindly,share your thoughts on this and feel free to point out if any bugs are found.

Thanks & Regards,

Munender

Sr.ServiceNow-Developer & Implementation Expert

image

View original source

https://www.servicenow.com/community/itsm-articles/downloading-excel-file-on-browser/ta-p/2312208