ServiceNow lists contain a lot of very nice capability that allow you to display and access data in a lot of different ways. One of these is the ability to right-click a list header and export the list data in a variety of formats…for external reporting or otherwise. For example, this is particularly useful as an admin when you need to export a list of records to XML and move them to another ServiceNow instance.

While this functionality works great on standard lists, it is completely absent on related lists at the bottom of records displayed in the form view. I can’t count the number of times I wished I could export a filtered related list in the same way you can with a standard list. Fortunately, with a few slight adjustments to some context menu actions in the system, there is a way!

Related List Export

The Solution…

Enabling export capability for related list records in your system can be accomplished by creating some new records in the ‘System UI -> UI Context Menus’ module in your left nav. These records are based on their existing, standard list counterparts so I can show you how to add the ‘Export -XML’ option here and you can add additional options for other formats as needed. Below is the configuration setup for the ‘Export’ menu container and the ‘XML’ export menu option.

‘Export’ UI Context Menu
Table: Global
Menu: List header
Type: Menu
Name: Export
Order: 1045
Active: True
Run onShow Script: True
Condition: ListProperties.isRelatedList() && !ListProperties.isRefList()
onShow Script:

/**
* This client-side script is used to dynamically change the context menu before it is displayed.
* It is called before showing the context menu if the Run onShow script flag is checked.
*
* The following variables are available to the script:
* 'g_menu' the context menu that is about to be shown
* 'g_item' the current context menu item
* 'g_list' the GlideList2 that the script is running against (only valid for List context menus)
* 'g_fieldName' the name of the field that the context menu is running against (only valid for List context menus)
* 'g_fieldLabel' the label of the field that the context menu is running against (only valid for List context menus)
* 'g_sysId' the sys_id of the row or form that the script is running against
*/
/*Hide the 'Export' context menu for defined related lists
No 'query' parameter available for accurate exports*/

if(g_list.getListName().indexOf('.REL:') > -1){
g_menu.setHidden(g_item);
}

Once you’ve added the ‘Export’ menu option for related lists as shown above, you can add options for the specific types of export options within the menu. Here is the setup for the ‘XML’ export option which you can add by navigating to ‘System UI -> UI Context Menus’ in your left navigation pane. Make sure to put it under the correct Parent menu so that it shows up where you expect!

‘XML’ UI Context Menu
Table: Global
Menu: List header
Type: Action
Name: XML
Parent: Export (Reference the record you just created above)
Order: 300
Active: True
Run onShow Script: False
Condition: gs.hasRole(‘admin’);
Action Script:

/**
* Script executed on the Client for this menu action
*
* The following variables are available to the script:
* 'g_list' the GlideList2 that the script is running against (only valid for List context menus)
* 'g_fieldName' the name of the field that the context menu is running against (only valid for List context menus)
* 'g_sysId' the sys_id of the row or form that the script is running against
* 'rowSysId' is also set to the sys_id of the row to support legacy actions, but g_sysId is preferred
*/

runContextAction();function runContextAction() {
/*Alert if context menu for defined related lists
No 'query' parameter available for accurate exports*/

if(g_list.getListName().indexOf('.REL:') > -1){
alert('Related list exports are not supported for custom defined related lists.');
return false;
}
var relField = g_list.getRelated().split(".");
var sysparm_rows = g_list.grandTotalRows;
var num_rows = parseInt(sysparm_rows);
var sysparm_query = relField[1] + "=" + gel('sys_uniqueValue').value + "^" + g_list.getQuery({orderby: true, fixed: true});

var sysparm_view = g_list.view;
if (num_rows < g_export_warn_threshold) {
var dialog = new GwtPollDialog(g_list.tableName, sysparm_query, sysparm_rows, sysparm_view, 'unload_xml');
dialog.execute();
return;
}
var dialog = new GwtExportScheduleDialog(g_list.tableName, sysparm_query, sysparm_rows, sysparm_view, 'unload_xml');
dialog.execute();
}

That should be it! Once you’ve added the UI context menus as shown above you’ll be able to export from any related list in the system (with one exception noted below). I think that the most common use case is XML exports for admin users. If you want to enable other exports, you can combine the ‘XML’ setup provided above with the out-of-box Context menu items for CSV, XLS, or PDF.

One Exception!!!

I believe one of the reasons that this functionality isn’t included by default is that there are some related lists in the system (Defined Related Lists) that this won’t work for. The reason that Defined related Lists don’t support exports is that they don’t include a query/filter component that you can get to via the context menu. Because of this, you’ll end up exporting all of the records in the table instead of the filtered related list.

I’ve included checks in the UI Context Menu records above to disable this functionality on defined related lists in your system. While these types of lists are extremely useful they’re also much less common so it isn’t much of an issue. Here are a couple of links to help you understand more about Defined Related Lists in ServiceNow.

http://www.servicenowguru.com/system-definition/relationships/defined-related-lists/
http://wiki.servicenow.com/index.php?title=Creating_Defined_Related_Lists