Hi All,
Trying to use Script Runner to create new subtasks and populate fields based on a screen that pops up during a workflow transition plus copy of fields held on the parent but can't work out how to set customfields using the subtask.
Managed to get the inbuilt fields, e.g. summary, assignee etc all working but now trying to start setting custom fields but failing :-(
// getting the custom fields
def actionSummaryFieldName = 'customfield_10101'
def actionAssigneeFieldName = 'customfield_10102'
def actionDescriptionFieldName = 'customfield_10104'
def actionDueDateFieldName = 'customfield_10103'
// getting the specified custom field values
def actionSummaryValue = get("/rest/api/2/issue/${issue.key}?fields=${actionSummaryFieldName}")
.header('Content-Type', 'application/json')
.asObject(Map)
def actionAssigneeValue = get("/rest/api/2/issue/${issue.key}?fields=${actionAssigneeFieldName}")
.header('Content-Type', 'application/json')
.asObject(Map)
def actionDescriptionValue = get("/rest/api/2/issue/${issue.key}?fields=${actionDescriptionFieldName}")
.header('Content-Type', 'application/json')
.asObject(Map)
def actionDueDateValue = get("/rest/api/2/issue/${issue.key}?fields=${actionDueDateFieldName}")
.header('Content-Type', 'application/json')
.asObject(Map)
// writing the custom field values to the subtask fielads
subtask.fields.summary = actionSummaryValue.body.fields[actionSummaryFieldName]
subtask.fields.assignee = actionAssigneeValue.body.fields[actionAssigneeFieldName]
subtask.fields.description = actionDescriptionValue.body.fields[actionDescriptionFieldName]
subtask.setCustomFieldValue(actionDueDateFieldName, Date.parse("yyyy-MM-dd", actionDueDateValue.body.fields[actionDueDateFieldName]))
//Getting the custom fields
def customFields = get("/rest/api/2/field")
.asObject(List)
.body
.findAll { (it as Map).custom } as List<Map>
def actionSummaryFieldId = customFields.find { it.name == 'Action Summary' }?.id
def actionAssigneeFieldId = customFields.find { it.name == 'Action Assignee' }?.id
def actionDueDateFieldId = customFields.find { it.name == 'Action Due Date' }?.id
def actionDescriptionFieldId = customFields.find { it.name == 'Action Description' }?.id
// clearing the fields used to create the Action
def clearFieldResult = put("/rest/api/2/issue/${issue.key}")
.queryString("overrideScreenSecurity", Boolean.TRUE)
.header('Content-Type', 'application/json')
.body([
fields:[
(actionSummaryFieldId): '',
(actionDescriptionFieldId): '',
(actionAssigneeFieldId): null,
(actionDueDateFieldId): null,
]
]).asString()
assert clearFieldResult.status == 204
Gave a quick look to your script. You are close.
def actionSummaryFieldId = customFields.find { it.name == 'Action Summary' }?.id
def actionAssigneeFieldId = customFields.find { it.name == 'Action Assignee' }?.id
def actionDueDateFieldId = customFields.find { it.name == 'Action Due Date' }?.id
def actionDescriptionFieldId = customFields.find { it.name == 'Action Description' }?.id
These variable will have custom field ids like customfield_xxxxx, not the values stored on the issue.
You need to further add more code to fetch values of those fields using a code like this.
def response = get('/rest/api/3/issue/bt-28') .header('Content-Type', 'application/json') .asObject(Map)
def customFieldValue = response.body.fields.customfield_xxxxx
Once you fetch the values the use it to create your issue.
You are close, just few more lines of code and you are done :)
I hope it helps.
Ravi
..and you don't need to make multiple rest calls to fetch individual custom field values. Once call should be enough.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.