How to export data to CSV from a widget
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2023 07:01 AM
I have a widget and it shows data from a database view based on the filter provided in the widget. The widget also adds calculative columns and data. The calculations are pretty complex and we can't do it as part of report (this is because report provides very basic calculations).
The widget is clone of OOB data.table widget. As you may know, it uses data.list array to load data.
As per requirements, we are able to show the data, however, I do need a functionality / button so that user can export the data in the array (data.list) to CSV file.
Note that this is not a traditional ServiceNow table which user can export by right click as there are some calculated column - which does not exist in the database view.
Considering this, it seems I may have to use (if available) Angular function to export if not then I will go with Javascript function.
Any pointer/reference/idea on how to do that?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2023 07:45 AM
If you expect users that do the export to have ACL access to the data, you could just navigate to a URL of the form:
https://<instance name>.service-now.com/<table name>_list.do?CSV&sysparm_query=<encoded query applied to the list>
Any <table name>_list.do or <table name>.do path followed by special parameter CSV at any position (with no value) will export the list or record as CSV - using the default list/form view.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2023 09:49 AM
The problem is that I have some calculated values and they do not exist in the view. So, simple view export will not work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2023 10:35 AM - edited ‎01-12-2023 10:44 AM
You could try creating a solution based on Blob(). Running the code:
(function () {
var csv = new Blob(['Col_1,Col_2,Col_3\r\nData 1,Data 2,Data 3\r\nData 4,Data 5,Data 6\r\n'], { 'type': 'text/csv', });
var csvURL = URL.createObjectURL(csv);
window.open(csvURL);
})();
in a browser console will offer for download a CSV file.
And here's the so downloaded file opened in LibreOffice:
Of course you would need to create a code that generates the CSV text; this should not be a problem as the structure is pretty str8 fwd.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2023 10:36 AM - edited ‎01-20-2023 10:37 AM
I believe you are referring to write this in Client Script section. The problem is that, the array is in Server side, so is there any way we can achieve this in server-side code? I tried your snippet and it throws error:
js_includes_sp.jsx?v=12-05-2022_2132&lp=Tue_Sep_27_19_30_45_PDT_2022&c=20_200:47460 Server JavaScript error "Blob" is not defined.
Another approach is to generate CSV string in serverside and pass it to the client script. However, I am just worried about the size of the string being passed from Server to Client.