How to Add Advanced Reporting to a List View Context Menu

Introduction

ServiceNow is a handy tool that shows reports which are useful to the user in the list view. The following are standard when you select a column which is groupable.

What can be useful is to add different report types to this list with which you can have more advanced use for the ITIL user. Here we give a rundown of the features we like the most. Below is an updated set that you can use to deploy to your own instance.

Adding new report types

When you navigate to the Context UI Menu module, you will be able to find the items shown above. Adding more items is quite easy with the following guidance.

List report

Name: List Report
Menu: List header
Type: Action
Order: 1019
Condition: !ListProperties.isRelatedList() && !ListProperties.isRefList() && (new GlideReportChoiceList).permitted(ListProperties.getTable())
Action script:

runContextAction();

function runContextAction() {
   var url = new GlideURL('sys_report_template.do');
   url.addParam('sysparm_title','List report on ' + g_list.getTitle());
   url.addParam('sysparm_type', 'list');
   url.addParam('sysparm_table', g_list.getTableName());
   url.addParam('sysparm_from_list', 'true');
   url.addParam('sysparm_manual_labor', 'true');
   url.addParam('sysparm_query', g_list.getQuery({orderby: true, fixed: true}));
   
   window.location = url.getURL();
}

onShow script: 
//ServiceNow blocks for end users the list report, therefore making it disabled for them. You can choose also other roles if needed.
if(!g_user.hasRole('itil')){
   g_menu.setDisabled(g_item);
}

Pareto report

Name: Pareto report
Menu: List header
Type: Action
Order: 1018
Condition: !ListProperties.isRelatedList() && !ListProperties.isRefList() && (new GlideReportChoiceList).permitted(ListProperties.getTable())
Action script:

runContextAction();

function runContextAction() {
   var url = new GlideURL('sys_report_template.do');
   url.addParam('sysparm_title','Pareto report on ' + g_list.getTitle() + ' grouped by ' + g_fieldName);
   url.addParam('sysparm_field', g_fieldName);
   url.addParam('sysparm_type', 'pareto');
   url.addParam('sysparm_table', g_list.getTableName());
   url.addParam('sysparm_from_list', 'true');
   url.addParam('sysparm_chart_size', 'large');
   url.addParam('sysparm_manual_labor', 'true');
   url.addParam('sysparm_query', g_list.getQuery({orderby: true, fixed: true}));
   
   window.location = url.getURL();
}

onShow script:
/**
 * Disable sorting if element is not sortable
 */
setSortable();
setGroupState();

function setSortable() {
   var hdrCell = g_list.getHeaderCell(g_fieldName);
   if (!hdrCell)
      return;

   var sortable = (getAttributeValue(hdrCell, 'sortable') == "true");
   if (sortable)
      g_menu.setEnabled(g_item);
   else
      g_menu.setDisabled(g_item);
}

// disable is this field is not groupable
function setGroupState() {
   var hdrCell = g_list.getHeaderCell(g_fieldName);
   if (!hdrCell)
      return;
   
   var groupable = (getAttributeValue(hdrCell, "groupable") == "true");
   if ((!groupable) || (g_list.getGroupBy() == ("GROUPBY" + g_fieldName)))
      g_menu.setDisabled(g_item);
   else
      g_menu.setEnabled(g_item);
}

Results

In the end you will get something like this in your list view:

The List report will show your query with your selected fields in the report with which you can schedule, share or add additional information (in accordance with roles).

The Pareto report will show you and other users, an overview of your report grouped on the field you’ve selected.

About the Author

Levi Verbeke is an experienced ServiceNow architect, PM and BA with over eight years of ServiceNow architectural and development background. He has worked with various applications and integrations within ServiceNow. Moreover, he has worked on numerous ServiceNow projects for large multinational companies in Belgium, Germany, Netherlands, the UK and Switzerland.

Updates

Blog