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