See this in action in our Sanbox https://radbee-sandbox.atlassian.net

ScriptRunner Scripted Fields in Jira Snapshots

See this use-case live on our sandbox environment here: https://radbee-sandbox.atlassian.net/wiki/spaces/DEVT/pages/157089959/Cost+Overview

 

In this Jira Snapshot example, the Development Issues table lists Jira tasks, or issues, with subtasks for each task listed in the second level.

The budgeting system in this case requires that, in order to calculate costs for a issue, each subtask must have an assigned cost. These subtask costs are actual, entered values, and the sum of subtasks costs will be calculated and assigned to the task.

Here are the Scripted Fields for this task, and the costs are calculated on the right. The $3,900 cost is the sum of the cost of the $2,400 and $1,500 subtasks.

The sum calculation itself is created in the “Calculated Cost” scripted field.

“Calculated cost” is a scripted field, summing up the “Cost” fields of subtasks

Prerequisites

This use case assumes that ScriptRunner for Jira is installed on the Jira instance (from which the Jira Snapshots are taken)

How to Do It:

  1. In Jira, set up your ScriptRunner scripted field. Follow the instruction here: https://docs.adaptavist.com/sr4jc/latest/features/scripted-fields

    1. This is the code snippet for the “Calculated cost” field:

      // sum up the values of this custom field final customFieldName = 'Cost' final parentIssueKey = issue.key as String final customFieldId = getFieldIdByName(customFieldName) List subTasksKey = getSubTasksKeyByIssue(parentIssueKey) as List // if the issue doesn't have any sub-tasks or is a subtask itself then no need for action if (subTasksKey.empty) { return } def firstSubTask = getIssueByKey(subTasksKey[0]) as Map if (!existFieldInIssue(firstSubTask.key as String, customFieldId)) { def fields = firstSubTask.fields as Map def issueType = fields.issuetype as Map def project = fields.project as Map logger.info "Custom field with name $customFieldName is not configured for issue type ${issueType.name} and project ${project.key}" return } def sum = subTasksKey.sum { subTaskKey -> def subtask = getIssueByKey(subTaskKey as String) as Map if (subtask.fields[customFieldId]!=null) subtask.fields[customFieldId] else 0 } return sum assert result.status == 204 String getFieldIdByName(String fieldName) { def customFieldObject = get('/rest/api/2/field') .asObject(List) .body .find { (it as Map).name == fieldName } (customFieldObject as Map).id } List getSubTasksKeyByIssue(String parentIssueKey) { def parentIssue = getIssueByKey(parentIssueKey) as Map def fields = parentIssue.fields as Map def subtasks = fields.subtasks as List<Map> subtasks*.id } Map getIssueByKey(String issueKey) { def result = get("rest/api/2/issue/$issueKey") .header('Content-Type', 'application/json') .asObject(Map) assert result.status == 200: result.body result.body } Boolean existFieldInIssue(String issueKey, String fieldId) { def issue = getIssueByKey(issueKey) def issueFields = issue.fields as Map issueFields.containsKey(fieldId) }
  2. Follow the instructions here: https://radbeedocs.atlassian.net/wiki/spaces/JSICC/pages/2177564674 to add this field to the available fields in Jira Snapshots.

  3. To create the Cost Overview table in Confluence, navigate to the page where this table should appear, and enter EDIT mode.

  4. In the top editor toolbar, click the “+” icon and type “jira s” in the search bar.  Then select the “Jira Snapshots” macro.

     

  5. In the “Edit Jira Snapshots Macro” overlay:

    1. Enter a title in the “Level title” field to represent the first level or “list” of Jira issues.

    2. Enter a query in the “Search JQL” field to limit the scope of issues, such as:

      1. project = DEVT AND issuetype in standardIssueTypes() order by created DESC
      2. In the “Add fields to display” field, select the desired columns, including the ScriptRunner scripted field.

    3. Add the next level, so that the individual sub-tasks are also shown in the table. You’ll need to select “+ Add new level.”

    4. Enter a title for the 2nd level.

    5. Enter a query in the “Search JQL” field, to retrieve all sub-tasks:

      1. parent=$key
    6. In the “Add fields to display” field, select the desired columns.

       

    7. Click the “Insert” button at the bottom right, to complete the macro’s configuration.

  6. Click the “Publish” button at the top right of the page.

  7. Finally, click the “Take New Snapshot” button to generate a static list of issues.