Generation script examples

The generation script creates the Summary Set and Report Summary Line database records that are used by the chart rendering code to produce a chart. The Summary Set and Report Summary Line tables are described here.

Example 1: First level locations script

This code is basic high level code to set up some variables to perform the following tasks:
  • Call a function to get the locations from the incident table.
  • Create location lines, line items, and location items.
  • Place the sys_id of the new summary set into the custom generation record.
  • Update the previous summary to expire now.

Each generation script must end with the three lines that set the summary id value (current.summary), set workflow off to avoid recursion on generation, and update the current record.

//
var g_sequence = 0;
var g_colors = new GlideChartFieldColors('incident', 'location');
var sid = fll_createSummary();
var xx = new GlideRecord('cmn_location');
xx.addNullQuery('parent');
xx.query();
while (xx.next()) {
   fll_createLine(xx, sid);
}
var oldSummary = current.getValue('summary');
current.summary = sid;
current.setWorkflow(false);
current.update();
fll_updatePreviousSummaryToExpireNow(oldSummary);

fll_createSummary Function

The fll_createSummary function creates a new summary set record that will be used to record the details for first level locations.

function fll_createSummary() {
   var s = new GlideRecord("sys_report_summary");
   s.title = "First Level Locations";
   s.field = "location";
   s.table = 'incident';
   s.grouping = 'location';
   return s.insert();
}

fll_createLine Function

The fll_createLine function records the locations of an incident by creating a report summary line and associating it with the summary set.

function fll_createLine(location, summaryID) { 
   if (location.name == 'Earth')
      return;
  
   var s = new GlideRecord("sys_report_summary_line");
   s.summary = summaryID;
   s.sequence = g_sequence++;
   s.category = location.sys_id;
   s.name = location.name;
   s.insert();
   fll_createLineItems(location, summaryID);
}

fll_createLineItems Function

The fll_createLineItems function creates the line items in the database according to the locations.

function fll_createLineItems(location, summaryID) {
   var inc = new GlideAggregate('incident');
   inc.addQuery('active=true');
   inc.addQuery('location.full_name', 'STARTSWITH', location.full_name.toString());
   inc.addAggregate('COUNT', 'location');
   inc.query();
   while (inc.next()) {
      fll_createLocationItem(location, inc, summaryID);
   }
}

fll_createLocationItem Function

The fll_createLocationItem function creates the location items in the database according to the locations.

function fll_createLocationItem(location, inc, summaryID) {
   var s = new GlideRecord("sys_report_summary_line");
   s.summary = summaryID;
   s.sequence = g_sequence++;
   s.level = 1;
   s.value = inc.getAggregate('COUNT', 'location');
   s.category = inc.location;
   s.name = inc.location.getDisplayValue();
   s.color = g_colors.get(inc.location.toString());
   s.query_text = "active=true^location=" + inc.location.toString();
   s.insert();
}

fll_updatePreviousSummaryToExpireNow Function

The fll_updatePreviousSummaryToExpireNow function updates the previous summary to expire now.

function fll_updatePreviousSummaryToExpireNow(summaryID) {
	if (summaryID == null)
		return;
 
	var previous = new GlideRecord("sys_report_summary");
	previous.get(summaryID);
	if (previous.isValidRecord()) {
		previous.expires = gs.nowDateTime();
		previous.update();
	}
}
Figure 1. First level locations

Example 2: Trend of open incidents generation script

This example creates the chart data necessary to show the trend of open incidents created by month. Notice that the script has the standard three ending lines as documented in the previous example and that it uses the server script include called SummaryTableWriter to generate the data.

var opened = new SummaryTableWriter("incident", "");
  opened.setTitle("Trend of Open Incidents");
  opened.setTrend('opened_at', 'month');
  current.summary = opened.generate();
  current.setWorkflow(false);
  current.update();