How to add Option for Export form data in to Excel

pawan kalyan
Tera Contributor

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.

 

 

3 REPLIES 3

Tejas1018
Tera Contributor

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);
}

Tejas1018
Tera Contributor

 

To create an "Export to Excel" option for a form in ServiceNow, follow these steps:

  1. Navigate to "System UI" > "UI Actions" and click "New."
  2. Fill in the fields: Name it "Export to Excel," choose the relevant table (e.g., Incident [incident]), set the order, and ensure visibility.
  3. Submit the UI action.
  4. 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;
}

Hi 

 
 
 i tried that you shared above but its not working can you please define clearly what things need to be defined and checked in ui action. If possible please provide screenshots.
 
 
 
Also, let me know do i need to use both UI action and Business rule at a time. Even i tried giving both but its not working. it will be helpful if you share earliest. great thanks for sharing above details.