Snippets

Adaptavist Jira Cloud Set Assignee Based On A User Picker Field On Create Transition

Created by Kristian Walker last modified
/*
 * "This script should be placed as the last post function in the create transition in order to set the assignee field to the value of a user picker field. 
 * All right, title and interest in this code snippet shall remain the exclusive intellectual property of Adaptavist Group Ltd and its affiliates. Customers with a valid ScriptRunner 
 * license shall be granted a  non-exclusive, non-transferable, freely revocable right to use this code snippet only within their own instance of Atlassian products. This licensing notice cannot be removed
 * or amended and must be included in any circumstances where the code snippet is shared by You or a third party." 
 */

//get the issue key
def issue = issue.key

// Specify the name of the  list field to set
def userPickerFieldName = "<UserPickerFieldNameHere>"

// Get the Approver Custom field to get the option value from
def customField = get("/rest/api/2/field")
        .asObject(List)
        .body
        .find {
            (it as Map).name == userPickerFieldName
        } as Map
        
// Check if the custom field returns a valid field and is not null
assert customField != null : "Cannot find custom field with name of: ${userPickerFieldName}"

// Get the issue object to get the field value from
def result = get('/rest/api/2/issue/' + issue)
        .header('Content-Type', 'application/json')
        .asObject(Map)

// Get the accountID value from the custom user picker field
def assigneeValue = result.body.fields.(customField.id).accountId

//logger.info('Assignee Value is' + assigneeValue)

def updateResult = put('/rest/api/2/issue/' + issue)
        .header('Content-Type', 'application/json')
        .body([
        fields:[
                // Update the assignnee to the value of the custom user picker field. We are using the account ID so that the format is compliant with the upcoming GDPR changes.
                assignee:[id:assigneeValue]
        ]
])
        .asString()

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.