Forums

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

Get value from variable Custom Field

Daniel Alonso
Contributor
January 21, 2022

Hi,

I want to get some custom field values, but I reference to the custom field name with variables, this is my current code:

def issue = get("/rest/api/2/issue/${issueKey}")
.header('Content-Type', 'application/json')
.asObject(Map)
.body as Map
def customFields = get("/rest/api/2/field")
.asObject(List)
.body
.findAll { (it as Map).custom } as List<Map>

def cf = "PS1N1"

def cf_id = customFields.find { it.name == cf }?.id

return issue.fields.cf_id

 I want to get the value from "issue.fields.customfield_12345" but I need the 12345 to be variable because I'm planning to have all the custom fields I need to check with a loop.

Any ideas?

1 answer

1 accepted

0 votes
Answer accepted
Daniel Alonso
Contributor
January 21, 2022

Answering my own question, this is the solution:

def issue = get("/rest/api/2/issue/${issueKey}")
.header('Content-Type', 'application/json')
.asObject(Map)
.body as Map
def customFields = get("/rest/api/2/field")
.asObject(List)
.body
.findAll { (it as Map).custom } as List<Map>

def cf_fields = ["PS1N1", "PS1N2", "PS1N3"]

for (field in cf_fields){
def cf_id = customFields.find { it.name == field }?.id
logger.info(issue.fields.get(cf_id).toString())
}

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, team ’25 europe, early bird extension, conference discount, barcelona, hybrid event, AI teamwork, early bird pass, digital access, collaboration conference, atlassian events, register now, developer conference

1 week left to save on your Team '25 Europe pass

Still thinking about joining us for Team '25 Europe? Early bird has been extended for just one week! Now’s the time to lock in your lowest rate. Use code TEU25COMM-20 to save 20% at checkout. Hurry, this only lasts for 1 more week.

Learn more
AUG Leaders

Atlassian Community Events