Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Using Groovy script to display fields based on another fields value

inayatulla.shaik
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 18, 2019

Hi, I have just started my journey with JIRA. I have created two custom fields. So my task is that I suppose to display one field based on other field value. Can some one help me out groovy script in this regard.

2 answers

0 votes
Lachlan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 19, 2019

Hi @inayatulla.shaik 

I have an idea of a Script that can be modified to do what you ask, but the basics are there.

 

def getCustomVal(issueKey, customField) {
// Fetch the issue object from the key
def issueOne = get("/rest/api/2/issue/${issueKey}")
.header('Content-Type', 'application/json')
.asObject(Map).body

def issueOneFields = issueOne.fields as Map

return issueOneFields[customField] ?: 0
}

def issueKey = "" //Some issue key string
def customFieldOne = "" // Some custom field string
def customFieldTwo = "" // Some other custom field string
def valueOne = getCustomVal(issueKey, customFieldOne)
def valueTwo = getCustomVal(issueKey, customFieldTwo)

// Now perform some operation

def newVal = valueOne + valueTwo

// Perform a comparison

if (valueOne > valueTwo) {

return valueOne
}
else {

return valueTwo

}

Lachlan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 19, 2019

Once you get the custom field values, and do whatever operation you want with them, there are other options as well.

For example, you could modify ScriptRunner's example script for creating a task to create a new task with the result from above.

For reference, that example code is this:

def projectKey = 'TP'
def taskType = get('/rest/api/2/issuetype').asObject(List).body.find { it['name'] == 'Task' }['id']

post('/rest/api/2/issue')
.header('Content-Type', 'application/json')
.body(
[
fields: [
summary : 'Task Summary',
description: "Don't forget to do this!.",
project : [
key: projectKey
],
issuetype : [
id: taskType
]
]
])
.asString().body
0 votes
Ben Poulson
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 19, 2019

Do you have the Scriptrunner add on installed? It has a feature called "Scripted Fields" that can do this.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events