Change excel table export file name

Joe B2
Giga Guru

Has anyone come across/needed to change the default file name for an excel table export to not be the table name and be something custom instead?

 

The use case here is a custom app I have created for customers to complete staff pay assessments, calculate increases, overpayments, etc. As part of this the companies using it want to be able to download a copy of their

data into excel. This is all up and running and a database view has been created and the following function used to trigger the download from a button in the portal.

 

query = "u_hidden=false^u_hr_case=" + $scope.selectedAccount.hr_case_id + '^ORDERBYtcp_u_surname';
view = 'export';
table = 'sn_hr_core_assessment_download';

if (query != '' && view != '' && table != '') {
    var t = top.window.open("/" + table + ".do?EXCEL&sysparm_query=" + query + "&sysparm_view=" + view);
}

 

This works fine and has been in use for a little while but to tidy it up I would like to rename the export if possible BEFORE it hits the customer machine (so they have no need to rename it manually afterwards). 

 

Any ideas on how this could be achieved would be appreciated, thank you.

1 ACCEPTED SOLUTION

Ahana 01
Tera Expert


Yes, it is possible to change the default file name for an Excel table export in ServiceNow. However, it requires a bit of scripting and customization. Here are the steps:

1. Create a UI Action on the table you want to export.
2. In the UI Action, you can use the 'exportWithLabel()' function to export the table data.
3. The 'exportWithLabel()' function accepts two parameters: the format of the export file (in this case, 'xls') and the custom name you want to give to the file.
4. The custom name should be a string and it will be used as the file name for the exported Excel file.

Here is a sample code for the UI Action:

javascript
var exportName = 'CustomFileName'; // Replace with your custom file name
var table = current.getTableName(); // Get the table name

var exporter = new ExportWithLabel('xls', exportName);
exporter.exportRecords(table, current.getEncodedQuery());


Please note:

- This will only change the file name for the exports triggered by this UI Action. The default 'Export' option in the table will still use the table name as the file name.
- Be careful with the file name. It should not contain any characters that are not allowed in file names.
- This customization might not be upgrade-safe. If ServiceNow changes the way exports work in a future update, this customization might stop working.


nowKB.com

For a good and optimistic result, and solving ServiceNow-related issues please visit this website.https://nowkb.com/home
Kindly mark correct and helpful if applicable

View solution in original post

7 REPLIES 7

Ahana 01
Tera Expert


Yes, it is possible to change the default file name for an Excel table export in ServiceNow. However, it requires a bit of scripting and customization. Here are the steps:

1. Create a UI Action on the table you want to export.
2. In the UI Action, you can use the 'exportWithLabel()' function to export the table data.
3. The 'exportWithLabel()' function accepts two parameters: the format of the export file (in this case, 'xls') and the custom name you want to give to the file.
4. The custom name should be a string and it will be used as the file name for the exported Excel file.

Here is a sample code for the UI Action:

javascript
var exportName = 'CustomFileName'; // Replace with your custom file name
var table = current.getTableName(); // Get the table name

var exporter = new ExportWithLabel('xls', exportName);
exporter.exportRecords(table, current.getEncodedQuery());


Please note:

- This will only change the file name for the exports triggered by this UI Action. The default 'Export' option in the table will still use the table name as the file name.
- Be careful with the file name. It should not contain any characters that are not allowed in file names.
- This customization might not be upgrade-safe. If ServiceNow changes the way exports work in a future update, this customization might stop working.


nowKB.com

For a good and optimistic result, and solving ServiceNow-related issues please visit this website.https://nowkb.com/home
Kindly mark correct and helpful if applicable

That looks promising, thank you. Shall give it a go and mark as the solution if it does the trick from the portal.

Thanks sir

I have tried to replicate the same and it did not work. Can you please share more specifications like clientSide/ServerSide , listCompactibilities etc.