Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Custom field condition with Scriptrunner cloud

E01807 April 16, 2019

I'd like to set a condition for a fast-track script based on the value of a custom field. I used to have this working in Scriptrunner (Server) but cannot find an example of how to work it using Scriptrunner (Cloud).

My successful old implementation was:

cfValues['Change Category']?.value == 'Fast Track'

All research done so far indicates I need to hard code the custom field ID to be something like (?!):

issueInput.fields.customfield_10107 == [value: '{{Fast Track}}'] as Map

where 10107 is the custom field ID. Which is not ideal as I'd like to refer to the name instead.

Any ideas?

2 answers

Suggest an answer

Log in or Sign up to answer
2 votes
Maximilian_Wassenberg
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!
October 28, 2019

I created this script and used it as condition in post function

//This script is for Scriptrunner Jira Cloud to get custom field value of current issue as condition in postfunction

package com.adaptavist.sr.cloud.samples.events

//define custom field ID from the field where you want to get the value of
def customFieldName = 'customfield_10046'

//create API request to get custom field value
def result = get("/rest/api/2/issue/${issue.key}?fields=${customFieldName}")
.header('Content-Type', 'application/json')
.asObject(Map)
if (result.status == 200) {
result.body.fields[customFieldName].value.toString() == "YOUR_CONDITION"
//if you need to check if the field value machtes YOUR_CONDITION and a assingee is set
//result.body.fields[customFieldName].value.toString() == "YOUR_CONDITION" && issue.fields.assignee != null
} else {
return "Error retrieving issue ${result}"
}

Marianne Lee _Nagarro_ December 28, 2019

Hi!

Thanks for sharing your script! I tried it and it works, despite getting a warning "[Static type checking] - No such property: value for class: java.lang.Object @ line 12, column 5." for 

result.body.fields[customFieldName].value.toString() == "YOUR_CONDITION"

 

Did you encounter this? How can this be rectified?

Regards,

Marianne

0 votes
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 24, 2019

Hi Tim,

Thank you for your question.

I can confirm that by default ScriptRunner for Jira Cloud will require you to specify setting the field by its ID due to the fact the Rest API in Jira cloud is required to specify fields by their ID's and the name cannot be used as Jira Cloud does not have a Java API like the server version does.

You can see more details on the differences between the cloud and server versions of the plugin in the documentation page here.

I can confirm that if you wanted to dynamically look up the ID of the field base on its name then you can do this by making a call to the field rest API to return the field based on its name.

I can confirm that we have an example of how to do this inside of the documentation page here where the customField rest call returns the field based on its name so that the field can be specified by its ID.  You will be able to use this example as a reference to show how you can look up fields based on their name.

If this response has answered your question can you please mark it as accepted so that other users can see it is correct when searching for similar answers.

Regards,

Kristian

TAGS
AUG Leaders

Atlassian Community Events