Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Update jira cloud custom field using scriptrunner

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

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

0 votes
Answer accepted

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

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

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

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

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.

Hi,

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

Cheers,
Jack

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you