Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Info

In this use case, you will learn how to use a simple script to get the values from the grid table and reuse these values in JQL and so on.

You will also learn how to use several features in TGNG app such as:

Connecting an external data source

Data Mirror

How to add option attributes to a list

How to fill in a list with dynamic options

Table of Contents
stylenone

Introduction

This article shows an example of how you can extract the grid data using the API and a scripted field in Jira

The scripted field is a custom field, provided by the Script Runner. To save the sum of the numbers in the grid column as a value of another custom field

Prerequisites

Post-condition

  • User can use the grid to update related information for the list of products

  • User can create issues using the Shopping List grid

  • User can download/upload the Configuration for personal usage

  • User can add more rows to fill the shopping list such as: total, quantity,..

  • User can view the total amount in the Total amount script field

Conditions

  • Required fields are marked with a red asterisk *

  • The grid must contain at least one column, or else it will show the pop-up message
    “Error table-config-column-config-required”.

  • The grid can be displayed in more than one project

  • User have to set the name of Column to be an exact match with Column of the database.

    User need to type the correct statement for the “SQL query”. For example:

    Code Block
    SELECT column1, column2, ... FROM table_name;

Flow chart diagram

Drawio
mVer2
zoom1
simple0
inComment0
custContentId103220412
pageId103710723
lbox1
diagramDisplayNameUntitled Diagram-1714637317676.drawio
contentVer6
hiResPreview1
revision6
baseUrlhttps://tablegrid.atlassian.net/wiki
diagramNameUntitled Diagram-1714637317676.drawio
pCenter1
aspectysmZWbln4f75sTEA6dzt 1
width541
linksauto
tbstyletop
height1601

Configure Grid and Script field

Basic Flow

Action

1

Click Add Data Source in Data Source

image-20240424-043311.png
2

Fill all required information in Data Source

image-20240424-044139.png
3

Click Test Connection to examine the connection between database and Jira

4

Click Save to save data source

5

In Table Grid section, choose Grids 

6

In Driving Table, click Add Driving Table button to add new driving table

image-20240424-045524.png
7

Fill information for “Name

In this example, user used

  • Name = “Products

8

In Configuration - Column, click Add new column to create new column

9

In Add Column, choose String in Column type

User fill information for Identifier and Title

Type “code“ as Identifier and type “Code” as Title

image-20240424-050127.png

Click Add to save the information 

10

In Add Column, choose String in Column type

User fill information for Identifier and Title

Type “name“ as Identifier and type “Name” as Title

image-20240424-050248.png

Click Add to save the information 

11

In Add Column, choose Number in Column type

User fill information for Identifier and Title

Type “price“ as Identifier and type “Price” as Title

image-20240424-050349.png

Click Add to save the information 

12

In Configuration - Data Mirror,

Select the Data Source that you created in step #4

Fill the Database table name

Note

The Database table name must be unique in your database

image-20240424-050545.png

Click Create table to create table in your database

13

Click Save to save the Driving Table Config

14

Click image-20240424-051445.png to add row in the Driving Table
Fill the product information into Driving Table

image-20240424-051939.png

Click image-20240424-052023.png to save the Driving Table

15

In Grid Fields, click Add Grid button to add new grid

image-20240424-044608.png
16

Fill information for “Name” and “Description” 

In this example, user used

  • Name = “Shopping List

  • Description = “Shopping List use case

image-20240424-045003.png
17

In Configuration - Column, click Add new column to create new column

18

In Add Column, choose Single Select List in Column type

User fill information for Identifier and Title

Type “jproduct“ as Identifier and type “Product” as Title

image-20240424-052302.png
Info

To display the price automatically, use the dynamic option mapping. You need to map the option label to display the product name and the option value to display the price. 

In Option attributes, click image-20240424-052712.png to add option attribute

Type “price” as Key

image-20240424-093702.png

In Dynamic options, chooseyour databasefor Data Source and type the SQL query:

Code Block
languagejs
SELECT name, price FROM products

Click Get data to get data from your database

image-20240424-054057.png

In Mapping:

  • for label, select “name”

  • for price, select “price”

image-20240424-095316.png

Click (blue star) to save the dynamic options

Click Add to save the information 

19

In Configuration - Column, click Add new column to create new column

20

In Add Column, choose Formula in Column type

User fill information for Identifier and Title

Type “jprice“ as Identifier and type “Price” as Title

image-20240424-070626.png

In Formula expression, type:

Code Block
languagejs
var result;
if($(jproduct).hasOwnProperty('optionObject')){
  result = $(jproduct).optionObject.price;
}else{
  result = $(jproduct).price;
}
return result;

Click Add to save the information 

21

In Configuration - Column, click Add new column to create new column

22

In Add Column, choose Integer in Column type

User fill information for Identifier and Title

Type “jquantity“ as Identifier and type “Quantity” as Title

image-20240424-073659.png

Click Add to save the information 

23

In Configuration - Column, click Add new column to create new column

24

In Add Column, choose Formula in Column type

User fill information for Identifier and Title

Type “jtotal“ as Identifier and type “Total” as Title

image-20240424-073936.png

In Formula expression, type:

Code Block
languagejs
return $(jprice)*$(jquantity)

Click Add to save the information 

25

Click Save to save the grid configuration 

image-20240424-082258.png
26

Navigate to ScriptRunner, choose Fields

27

Click Create Script Field

image-20240502-085109.png
28

Click Custom Script Field

image-20240502-085242.png
29

In Field Name, type “Total amount”

In Template, select Text Field (multi-line)

image-20240502-085519.png
30

In Script field, type the script

The script below helps to create a scripted custom field that calculates the sum of all numbers in the specified column of the TGNG grid

Code Block
languagegroovy
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.plugin.PluginAccessor;
import com.atlassian.jira.user.ApplicationUser;
import org.apache.log4j.Logger;
// set up logger
Logger log=Logger.getLogger("com.idalko.scripts");
// get an issue
String tgngCustomFieldName = "TGNG Grid"; //Change the grid custom field name to relevant one
String columnIdToSum = "price"; //Change the column ID to the relevant one
// get TGNG custom field
CustomFieldManager customFieldManager = ComponentAccessor.getOSGiComponentInstanceOfType(CustomFieldManager.class);
CustomField tgngCustomField = customFieldManager.getCustomFieldObjectsByName(tgngCustomFieldName).get(0);
Long tgngCustomFieldId = tgngCustomField.getIdAsLong();
ApplicationUser applicationUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
// read grid data
PluginAccessor pluginAccessor = ComponentAccessor.getPluginAccessor();
Class apiServiceClass = pluginAccessor.getClassLoader().findClass("com.idalko.tgng.jira.server.api.GridService");
def gridFieldData = ComponentAccessor.getOSGiComponentInstanceOfType(apiServiceClass);
def callResult = null;
try {
    callResult = gridFieldData.readFieldData(issue.getId(), tgngCustomFieldId, applicationUser, null);
} catch (Exception e) {
    log.error(e.getMessage())
}
def gridRows = callResult.getRows();
Double sum = 0;
for (row in gridRows) {
    def column = row.getColumns().get(columnIdToSum);
    sum += column.toDouble()
}
return sum;
Info

Note:

  • You need to change the grid custom field name and column ID in the script as below: 

    Code Block
    // configuration (update it to match your JIRA settings)
    String tgngCustomFieldName = "TGNG Grid";
    String columnIdToSum = "summary";
  • tgngCustomFieldName - the name of the TGNG grid custom field that will be used for calculation

  • columnIdToSum - a column identifier(a unique variable to identify the column) which will be used to get data for the summary calculation

image-20240502-102621.png

Info

Click on image-20240502-110644.png to view more example scripts

image-20240502-110758.png

31

Click Add to save the Script Field

Exceptional Flow

Users clicks “Cancel” at “Data Source”

Continued from step #2 in the Basic Flow:

Step

Description

3

The system will automatically come back to the “Data Source” page

Use case stops. 

Users type the wrong information about the data source

Continued from step #4 in the Basic Flow:

Step

Description

5

An “Errors” message appears 

Use case stops.

Users type wrong SQL query 

Continued from step #18 in the Basic Flow:

Step

Description

19

An “Errors” message appears 

Use case stops. 

Users click “Cancel” when saving the config

Continued from step #25 in the Basic Flow:

Step

Description

26

The system shows a pop-up message: “Exit without saving?”

27

User click Yes button

Use case stops. 

Users click “Cancel” when saving the script field

Continued from step #31 in the Basic Flow:

Step

Description

32

The system will automatically come back to the “Script Fields” page

Use case stops. 

Extract Total amount from Shopping List Grid

Create a Jira project

  1. Click Project on the Navigation bar

  2. Click Create project

  3. Fill required information to create project

image-20240424-084100.png

Add Grid and Script field on project screen

  1. Click Issues tab, in Screens section choose Screens

  2. Click Configure on your project

image-20240502-103234.png
  1. In Configure Screen, select Total amount& Shopping List as a custom field
    Click Add to add field on project screen
    Turn image-20240424-090222.png the field to show it on project screen even when empty

image-20240502-103742.png

Final result on issue screen

  1. Open an issue of your project, click Edit the Shopping List field

image-20240502-104507.png

  1. Click

image-20240502-105029.png to add row to the grid, select Product and type Quantity

image-20240502-104957.png
  1. Click image-20240502-105438.png to save the grid

image-20240502-105418.png
  1. Total amount is calculated automatically from Total values in Shopping List grid

image-20240502-105731.png