Getting value of a single custom field using REST API

Robert Wen_ReleaseTEAM_
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 26, 2021

I'm trying to create a script to run as as post-function and I'm a bit fuzzy on the Jira Cloud REST API.  Is there something that exists in the API that allows me to get the value of an issue's custom field?

I know the custom field ID, and Scriptrunner has as its context, the issue ID (I believe).  I just need to find how to extract the value for specific custom fields.

Thanks in advance!

3 answers

1 accepted

3 votes
Answer accepted
Prince Nyeche
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.
April 27, 2021

@Robert Wen_ReleaseTEAM_ 

Try the below code to get the field value

def key = issue.key

def field = issue.fields

def cfINeed = "customfield_12853"

// hold field value
def my_value

def result = get("/rest/api/2/issue/${key}")
.header('Content-Type', 'application/json')
.asObject(Map)

if (result.status < 300) {
logger.info("Success getting fields")
// field value
my_value = field[cfINeed]
} else {
logger.info("Error getting fields")
}
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 27, 2021

Hi Robert,

I can confirm that we have examples of how to extract values from different types of fields in the documentation page located here and you can run these examples on the Script Console page in order to see how to extract values from different type of fields.

You will be able to use these examples as a reference guide in order to help you to create the script that you require.

As a tip for the future, I can confirm the best way to see what structure a field uses is to set a value in the field on a test issue and then to run the Get Issue Fields example script on the Script Console as this will return the JSON structure for the issue and show how the data is stored for the field and you can use this to help see what structure you must specify to extract the value from a field on an issue.

I hope this information helps.

Regards,

Kristian

0 votes
Robert Wen_ReleaseTEAM_
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 26, 2021

I think I'm close.  I looked at https://community.atlassian.com/t5/Jira-questions/ScriptRunner-Cloud-Getting-fields-for-issue-takes-too-long/qaq-p/1597099 and it showed me how to specify the fields I want using queryString.

The static analyzer doesn't like the "value" in each statement for the fields.  Anyone have any experience with the call?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events