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
}