Export Excel via UI Action in List View

Kim46
Tera Contributor

Hi everyone,

I'm trying to implement an Excel export feature via a UI Action in the list view, but the script I've tried it doesn’t work as expected.

 

[Requirement]
While we’re aware that there is an out-of-the-box Excel export feature available via the list context menu (right-click), our client specifically requires the ability to export records through a UI Action (List Banner Button).

 

I referred to the following post and tried several script examples:
https://www.servicenow.com/community/developer-forum/how-can-i-replicate-export-as-excel-in-incident...

 

Here’s the script I’ve attempted to use:

function downloadAllToExcel() {
  var tableName = g_list.getTableName();
  var query = g_list.getQuery();
  var view = new URLSearchParams(window.location.search).get("sysparm_view") || "";

  var url = "/export/" + tableName + ".do?CSV" +
            "&sysparm_query=" + encodeURIComponent(query) +
            "&sysparm_view=" + encodeURIComponent(view);

  window.location.href = url;
}

 

 

However, when I click the button, I get the following error in the console:

 

 
Uncaught TypeError: Cannot read properties of null (reading 'location')
    at downloadAllToExcel
    at HTMLButtonElement.onclick

 

Has anyone encountered a similar issue or know how to properly implement this export function via a UI Action?

Any guidance would be greatly appreciated!

Thanks in advance!

 

3 REPLIES 3

SANDEEP DUTTA
Tera Patron
Tera Patron

Hi @Kim46 ,

You can check this link :

https://www.quora.com/How-do-I-create-a-custom-export-Excel-UI-action-in-related-lists

 

Thanks,
Sandeep Dutta

Please mark the answer correct & Helpful, if i could help you.

Ankur Bawiskar
Tera Patron
Tera Patron

@Kim46 

what debugging did you do?

did you add alert and see?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Kim46
Tera Contributor

Thank you for replying to my question. In the end, I was able to implement it. But only xls file. 

Here's it. 

function outputExcel() {

    var viewName = '<view name>’;

    var url = “<table name>_list.do?EXCEL&export_format=xlsx&sysparm_view=" + viewName;

    g_navigation.open(url);

}