How to add Option for Export form data in to Excel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2024 01:18 AM
Hi
i want to add a option like export to excel for a form can someone please provide a code to acheive this in servicenow.
for example: if i open incident form and right click on the top i am getting export to pdf and xml. but i want add export to excel option. once i click on export to excel form fields and data should get downloaded in excel.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2024 01:29 AM
To export data to Excel in ServiceNow, you have two methods. Firstly, create a UI Action by navigating to System Definition > UI Actions, then assign a name and select the target form. Paste the provided script in the Script field, replacing 'your_table_name' with the relevant table name. This script generates Excel content from form fields and facilitates a download via a temporary attachment. Alternatively, you can use a Business Rule. Set a condition script to ensure the form contains data for a specific field, then paste the UI Action script into the Run script field. These
methods streamline exporting form data to Excel for efficient data management.
code-
var gr = new GlideRecord('your_table_name');
gr.addQuery('sys_id', current.sys_id);
gr.query();
if (gr.next()) {
var excelData = '';
for (var fieldName in gr.fields) {
excelData += gr.fields[fieldName].getLabel() + '\t';
}
excelData += '\n';
do {
for (var fieldName in gr.fields) {
excelData += gr.getValue(fieldName) + '\t';
}
excelData += '\n';
} while (gr.next());
var fileName = current.getDisplayValue('number') + '.xlsx';
var content = new GlideRecord('sys_attachment');
content.initializeFrom(fileName, 'text/plain');
content.setValue('content', excelData);
content.insert();
gs.download(content.sys_id, fileName);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2024 01:31 AM
To create an "Export to Excel" option for a form in ServiceNow, follow these steps:
- Navigate to "System UI" > "UI Actions" and click "New."
- Fill in the fields: Name it "Export to Excel," choose the relevant table (e.g., Incident [incident]), set the order, and ensure visibility.
- Submit the UI action.
- Craft the script by pasting it into the "Onclick" field of the UI action.
(function() {
var gr = new GlideRecord('incident'); // Replace 'incident' with your table name
gr.get(current.sys_id); // Retrieve the current record
var excelData = '';
for (var fieldName in gr.fields) {
excelData += gr.fields[fieldName].getLabel() + '\t'; // Tab-separated values for header
}
excelData += '\n';
excelData += getRowData(gr); // Get row data
var fileName = gr.getDisplayValue('number') + '.xlsx'; // File name with incident number
var content = new GlideRecord('sys_attachment');
content.initializeFrom(fileName, 'text/plain');
content.setValue('content', excelData);
content.insert();
gs.download(content.sys_id, fileName); // Download the file
})();
function getRowData(gr) {
var rowData = '';
do {
for (var fieldName in gr.fields) {
rowData += gr.getValue(fieldName) + '\t'; // Tab-separated values for row data
}
rowData += '\n';
} while (gr.next());
return rowData;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2024 02:12 AM
Hi