Forums

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

Update jira cloud custom field using scriptrunner

Neil Ingram
Contributor
February 26, 2019

Hi, hope someone can help. I have some code to calculate the number of days between two dates and I would like to store the result in a custom field. However, no matter what i try I can't figure out how to populate the custom field. Everyone appears to work apart from updating the custom field. Please help!!

I'm using Jira Cloud and Scriptrunner Cloud.

See code I have created below, using customField_21122. I'm testing through the script console.

 

def issueKey = 'NIST-20'
def analysisFieldId = getFieldIdFromName('Created')
def readyFieldId = getFieldIdFromName('Ready to Play Date')

def analysisdate = new Date().parse('yyyy-MM-dd', getIssueField(issueKey, analysisFieldId))
def readyDate = new Date().parse('yyyy-MM-dd', getIssueField(issueKey, readyFieldId))
def dateDifference = analysisdate - readyDate
def dd = dateDifference + ' Days'

def result = put('/rest/api/2/issue/' + issueKey)
.header('Content-Type', 'application/json')
.body([
fields:[
customField_21122 = dd
]
])
.asString()
if (result.status == 204) {
return 'Success'
} else {
return "${result.status}: ${result.body}"
}

def getIssueField(issueKey, fieldId) {
def issueFieldValue = null
def result = get('/rest/api/2/issue/' + issueKey)
.header('Content-Type', 'application/json')
.asObject(Map)
if (result.status == 200) {
result.body.fields.each { key, value ->
if (key == fieldId) {
issueFieldValue = value.toString()
}
}
logger.warn issueFieldValue
return issueFieldValue
} else {
logger.warn "Failed to find issue: Status: ${result.status} ${result.body}"
return null
}
}

def getFieldIdFromName(fieldName) {
def customFieldObject = get('/rest/api/2/field').asObject(List).body.find { it.name == fieldName
}

2 answers

2 accepted

0 votes
Answer accepted
Kristian Walker _Adaptavist_
Community Champion
February 28, 2019 edited

Hi Neil,

Thank you for your question.

I can confirm that we have examples of setting the value of lots of different custom field types on an issue inside of the documentation pages located here, and that you will be able to use to these examples as a guide to show how you should get the custom field in your script and how to reference the field to set on the issue.

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

Neil Ingram
Contributor
February 28, 2019

Yes, thanks. I looked at the documentation pages and was eventually able to make it work. My issue turned out to be the custom field did not exist on a screen. Once I added it to a screen it worked. 

I don't really want the field on a screen, purely for reporting, so if there is a way to ignore whether the field is on a screen or not, that would be really helpful.

 

Thanks

Kristian Walker _Adaptavist_
Community Champion
February 28, 2019 edited

Hi Neil,

Thank you for your response.

I can confirm that it is possible to set the value for a field that is not on the screen by setting an overrideScreenSecurity query string parameter and I can confirm that we have an example of how to do this in the documentation located here.  

Please note that in order to use this you will need to ensure that the script is run as the ScriptRunner Add on user and not the current user.

Regards,

Kristian

Like • Neil Ingram likes this
Neil Ingram
Contributor
February 28, 2019

Hi Kristian,

Perfect, I had the override security code but wasn't running the script as the ScriptRunner Add on user.

Many thanks for the clarification.

Regards,

Neil

Payal Gogia June 3, 2021

Hi Kristain,

I want to populate the value of the custom field checklist using Script Runner (Jira cloud). But I am not able to do that. Can you please help me here or give me any direction.

Jack Hunter _HeroCoders_
Atlassian Partner
June 3, 2021

Hi,

The overwriteScreenSecurity works only when editing issues. For the "create" operation, the field must be present on the Create Issue screen.

Cheers,
Jack

0 votes
Answer accepted
Neil Ingram
Contributor
February 26, 2019

If I replace customField_21122 with Summary it works so it appears to be an issue specifically with custom fields.

Suggest an answer

Log in or Sign up to answer