Skip to Content

Export to CSV (List Action)

Export selected list records to CSV file with custom field selection.

List Button Table: incident
#ui-action #list-button #export #csv #excel #server-side #download

Script Code

JavaScript
1// Server-side code
2(function() {
3  // Get selected record sys_ids
4  var sysIds = g_request.getParameter('sysparm_record_list');
5
6  if (!sysIds) {
7    gs.addErrorMessage('No records selected');
8    return;
9  }
10
11  var recordIds = sysIds.split(',');
12
13  // Configuration: Define fields to export
14  var fieldsToExport = [
15    {name: 'number', label: 'Number'},
16    {name: 'short_description', label: 'Short Description'},
17    {name: 'state', label: 'State'},
18    {name: 'priority', label: 'Priority'},
19    {name: 'assignment_group', label: 'Assignment Group'},
20    {name: 'assigned_to', label: 'Assigned To'},
21    {name: 'caller_id', label: 'Caller'},
22    {name: 'opened_at', label: 'Opened'},
23    {name: 'sys_created_on', label: 'Created'}
24  ];
25
26  // Build CSV content
27  var csvContent = '';
28
29  // Add header row
30  var headers = [];
31  for (var i = 0; i < fieldsToExport.length; i++) {
32    headers.push(fieldsToExport[i].label);
33  }
34  csvContent += headers.join(',') + '\n';
35
36  // Add data rows
37  var gr = new GlideRecord('incident');
38  gr.addQuery('sys_id', 'IN', recordIds.join(','));
39  gr.query();
40
41  var rowCount = 0;
42  while (gr.next()) {
43    var row = [];
44
45    for (var i = 0; i < fieldsToExport.length; i++) {
46      var fieldName = fieldsToExport[i].name;
47      var value = '';
48
49      // Get display value for reference fields
50      if (gr[fieldName] && gr[fieldName].getDisplayValue) {
51        value = gr[fieldName].getDisplayValue();
52      } else {
53        value = gr.getValue(fieldName) || '';
54      }
55
56      // Escape values that contain commas or quotes
57      if (value.indexOf(',') !== -1 || value.indexOf('"') !== -1) {
58        value = '"' + value.replace(/"/g, '""') + '"';
59      }
60
61      row.push(value);
62    }
63
64    csvContent += row.join(',') + '\n';
65    rowCount++;
66  }
67
68  // Create attachment
69  var fileName = 'incident_export_' + new GlideDateTime().getDisplayValue().replace(/[^0-9]/g, '') + '.csv';
70
71  // Write to response
72  var response = g_response;
73  response.setContentType('text/csv');
74  response.setHeader('Content-Disposition', 'attachment;filename="' + fileName + '"');
75  response.getWriter().write(csvContent);
76
77  gs.info('Exported ' + rowCount + ' incident records to CSV');
78
79})();

How to Use

1. Create a new UI Action on your table 2. Set Name to 'Export to CSV' 3. Check 'List button' checkbox 4. Uncheck 'Client' checkbox 5. Set Order: 200 6. Customize fieldsToExport array with your desired fields 7. Add condition if needed 8. Test by selecting records and clicking Export 9. File will download automatically 10. Consider adding field validation and error handling

Explore More Scripts

Browse our complete library of ServiceNow scripts

View All Scripts