Forums

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

How can I write a script in Scriptrunner to check if there is a user assigned to a ticket and if not

Eric Blumberg November 14, 2018

// Check if ticket is assigned to a user

// -- If not, check that the user has the assignable user permission

// ----- If they do, assign it to the current user

if (issue.fields.assignee != null) {

//Set assignee here,

1 answer

1 accepted

0 votes
Answer accepted
Nir Haimov
Community Champion
November 15, 2018
def issueKey = 'AAA-111111'

def result = get('/rest/api/2/issue/' + issueKey + '?fields=assignee')
.header('Content-Type', 'application/json')
.asObject(Map)
if (result.status == 200){
if (result.body.fields['assignee'] == null) {
def put = put('/rest/api/2/issue/' + issueKey)
.header('Content-Type', 'application/json')
.body([
fields:[
assignee: [
id: 'admin'
]
]
])
.asString()
if (put.status == 204) {
return 'Success'
} else {
return "${put.status}: ${put.body}"
}
}
} else {
return "Failed to find issue: Status: ${result.status} ${result.body}"
}
Eric Blumberg November 15, 2018

@Nir can I get a brief explanation of the code?

Nir Haimov
Community Champion
November 15, 2018

Sure,

def result = get('/rest/api/2/issue/' + issueKey + '?fields=assignee')

this get only the "assignee" field data (no need to get all data if all you want is the assignee)

inside you got "if" statement, status 200 says "success"

so if success to get the assignee data of the issue, check if it null

if (result.body.fields['assignee'] == null)

if it is null, put (update) the assignee that you want (in my example i set 'admin'), you change the assignee to who ever you want. 

Eric Blumberg November 15, 2018

@Nir Haimov if I want the user who executed the transition to be set as the assignee is that the current user? How would I do so?

Like Deleted user likes this
Eric Blumberg November 15, 2018

Thank you for all your help

Suggest an answer

Log in or Sign up to answer