Using scripted fields to extract information from the grid using the table grid API

Introduction

Using a simple script, you can retrieve the values from the grid table and reuse these values in JQL and so on.

This script is implemented by using Scripted Fields. (This is functionality provided by the Script Runner ).

Let's suppose, that you want to save sum of the numbers in grid column as a value of another custom field.

Script

/** * scriptedFieldSum.groovy * * This is a script for Scripted custom field that calculates sum of all the numbers in specified column of the TGE grid. * * Configuration: * - tgeCustomFieldName - name of the grid custom field that will be used for calculation * - columnNameToSum - column name which will be used to get data for sum calculation */ import com.atlassian.crowd.embedded.api.User import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.security.JiraAuthenticationContext import com.atlassian.jira.user.ApplicationUser import com.atlassian.plugin.PluginAccessor import org.apache.log4j.Logger // set up logger Logger log = Logger.getLogger("com.idalko.scripts"); // configuration (update it to match your JIRA settings) String tgeCustomFieldName = "TGE"; String columnNameToSum = "isummary"; // find TGE custom field CustomFieldManager customFieldManager = ComponentAccessor.getOSGiComponentInstanceOfType(CustomFieldManager.class); CustomField tgeCustomField = customFieldManager.getCustomFieldObjectByName(tgeCustomFieldName); Long tgeCustomFieldIdLong = tgeCustomField.getIdAsLong(); // get current user JiraAuthenticationContext jiraAuthenticationContext = ComponentAccessor.getOSGiComponentInstanceOfType(JiraAuthenticationContext.class); Object userObject = jiraAuthenticationContext.getLoggedInUser(); User user = userObject instanceof ApplicationUser ? ((ApplicationUser) userObject).getDirectoryUser() : (User) userObject // read the grid data PluginAccessor pluginAccessor = ComponentAccessor.getPluginAccessor(); Class dataManagerClass = pluginAccessor.getClassLoader().findClass("com.idalko.jira.plugins.igrid.api.data.TGEGridTableDataManager"); def tgeGridDataManager = ComponentAccessor.getOSGiComponentInstanceOfType(dataManagerClass); Set<String> columnNames = new HashSet<String>(); columnNames.add(columnNameToSum); List<Map<String, Object>> gridData = new ArrayList<Map<String, Object>>(); try { def callResult = tgeGridDataManager.readGridData(issue.getId(), tgeCustomFieldIdLong, null, columnNames, 0, 10, user); gridData = callResult.getValues(); log.debug("Grid ID=" + tgeCustomFieldIdLong + " content: " + gridData + "\n"); } catch (Exception e) { log.debug("Grid ID=" + tgeCustomFieldIdLong + " data cannot be retrieved: " + e.getMessage() + "\n"); } // calculate sum double sum = 0; for (Map<String, Object> row : gridData) { Object rowValue = row.get(columnNameToSum); if (rowValue instanceof Map) { rowValue = ((Map) rowValue).get("value"); } Number rowNumber = null; if (rowValue instanceof Number) { rowNumber = (Number) rowValue; } else { String rowString = rowValue.toString(); rowNumber = Double.parseDouble(rowString); } sum += rowNumber.doubleValue(); } return sum;

At the beginning of the script you can see configuration section. The name of the TGE custom field and the name of the column which will be used for calculation of the sum are specified there. Change them to match your grid configuration.

// configuration (update it to match your JIRA settings) String tgeCustomFieldName = "TGE"; String columnNameToSum = "isummary";

This script uses TGE Java API, that was released in v1.18.0, so please ensure, that you are using fresh enough version of the TGE. If you cannot upgrade TGE for some reasons and have to use old version without Java API, please refer to the old approach.

Result