Forums

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

ScriptRunner Noob strugging to set customfield on subtask created as part of at transition

Stephen Renouf
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
December 22, 2020

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

1 answer

0 votes
Ravi Sagar _Sparxsys_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 22, 2020

Hi @Stephen Renouf 

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

Ravi Sagar _Sparxsys_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 22, 2020

..and you don't need to make multiple rest calls to fetch individual custom field values. Once call should be enough.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events