Munender Singh
Mega Sage

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" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:ui_form>
<label ><b>Release</b></label>

<g:evaluate var="jvar_sysid" object="true">
var val = RP.getWindowProperties().get('target_id');
val;
</g:evaluate>
<g:evaluate var="jvar_disp" object="true">
var disp = RP.getWindowProperties().get('target_disp');
disp;
</g:evaluate>
<g:ui_reference name="chg_id" id="chg_id"
table="change_request"
value="${jvar_sysid}"
displayvalue="${jvar_disp}"
query="active=true"
completer="AJAXTableCompleter"
columns="number"/>

<g:dialog_buttons_ok_cancel ok='triggerEmail()'/>
</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

Version history
Last update:
‎12-17-2019 02:04 AM
Updated by: