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
}
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
The overwriteScreenSecurity works only when editing issues. For the "create" operation, the field must be present on the Create Issue screen.
Cheers,
Jack
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If I replace customField_21122 with Summary it works so it appears to be an issue specifically with custom fields.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.