Forums

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

How to update Assignee based on Status update/changed

Kamal Iqlaas Ismail August 5, 2022

Hi everyone, hope all is well on your end.

With ScriptRunner listener, i am trying to get the Assignee updated whenever the ticket status gets updated. I see that in the History section, the Assignee did get updated, but it is not showing on the top left corner (UI) on the screen.

But when I update a field which i have tied to list of assignees based on the field values, the Assignee did got updated (both in History section and on the UI)

For the event mapping, i have set 

1. For issue created, i use ISSUE_CREATED_ID

2. For issue updated, i use ISSUE_UPDATED_ID

3. For status changed, i use ISSUE_GENERICEVENT_ID

Here is my code for updating the assignee:

if(event && event.eventTypeId == ISSUE_GENERICEVENT_ID){
log.debug("Status Updated!!")
Boolean isStatusChanged = isStatusChangedEvent(event, issue)
if(isStatusChanged){
log.debug("isStatusChanged=$isStatusChanged")
updateAssignee(issue)
}

}


Boolean isStatusChangedEvent(IssueEvent event, MutableIssue issue){

def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "status"}
if (change){
log.debug("isStatusChangedEvent")
return true
}else{
log.debug("isStatusChangedEventfalse")
return false
}
}
 

// Only fires when the eventTypeId is Generic Event
def
updateAssignee(MutableIssue issue){
String cf_field_object = getFieldObject(issue)
String cf_field_value = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject(cf_field_object))
String issueProject = issue.getProjectObject().key
String issueType = issue.issueType.name
String issueStatusRaw = issue.getStatus().name
String issueStatus = issueStatusRaw.toUpperCase()
String userID = ""

if (listProjects.contains(issueProject)){

if (issueType == "A") {
userID = "assigneeA"
} else if (issueType == "B"){
if (issueStatus == "statusB1"){
userID = "assigneeB1"
} else if (issueStatus == "statusB2"){
userID = "assigneeB2"
} else if (issueStatus == "statusB3"){
userID = "assigneeB3"
} else {
log.debug("Still same assignee")
return
}
}
}
ApplicationUser user = userManager.getUserByName(userID)
String userKey = user?.getName()
issueInputParameters.setAssigneeId(userKey)
}

Any pointers are greatly appreciated!

2 answers

1 accepted

1 vote
Answer accepted
Nic Brough -Adaptavist-
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.
August 5, 2022

This suggests a problem with indexing, and it's probably to do with the very last line of code, as that is intended to be used when people have typed/selected something in an edit screen.

Try issue.setAssigneeId(userKey)

Kamal Iqlaas Ismail August 6, 2022

Yes, I think so too, oddly enough after I posted this question, saw from another thread suggesting to use

issue.setAssigneeId

instead of 

issueInputParameters.setAssigneeId

 https://community.atlassian.com/t5/Jira-questions/Change-assignee-when-issue-moves-Script-runner/qaq-p/1018536

Appreciate the pointer @Nic Brough -Adaptavist- !

0 votes
Radek Dostál
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.
August 5, 2022

These two lines are odd

String userKey = user?.getName()
issueInputParameters.setAssigneeId(userKey)

 

setAssigneeId expects userkey - the variable name is right, but user.getName() returns username, not the key, I think the correct method is user.getKey()

 

The issueInputParameters itself seems in the wrong place, I suspect you're not including the whole method, or are defining it elsewhere?

 

Normally if you have a MutableIssue you could just do mutableIssue#setAssigneeId(String userkey), but this only changes the value in memory - you need to then update the issue in Jira via IssueManager, e.g.:

/*
event.issue should be "Issue" aka ImmutableIssue

this will return the MutableIssue object you can use to setAssigneeId on
MutableIssue mutableIssue = issueManager.getIssueObject(issue.getId())
*/

import
com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue

//Assuming "user" variable is your ApplicationUser
String userKey = user?.getKey() // will be e.g. "JIRAUSER012345"

if (userKey) {
IssueManager issueManager = ComponentAccessor.getIssueManager()
MutableIssue mutableIssue = issueManager.getIssueObject(event.issue.getId())

mutableIssue.setAssigneeId(userKey)

//user = who is doing the change, who will be shown in issue history as author of the update
//mutableIssue
//EventDispatchOption.ISSUE_UPDATED = event type to dispatch for this update
//false = whether to send email notifications for that event or not
issueManager.updateIssue(user, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)
}

 

However as you do refer to issueInputParameters I suspect there is other code in there somewhere. Both IssueInputParameters and MutableIssue are valid ways to update an issue, I just don't see the rest of the code and MutableIssue is simpler to use.

 

edit: added imports to the snippet, but you can use 'def' anywhere there and it'll work, just trying to show the difference between MutableIssue and Issue which is sometimes counter intuitive

edit2: sorry I felt off about this afterward and had to check in IDE, indeed I had a fault in the updateIssue method, now should be correct!

Kamal Iqlaas Ismail August 6, 2022

Appreciate the response, yes I did declare, just that forgot to include in the question.

Will try your suggestion. Thank you!

Long eventTypeId = event.eventTypeId

MutableIssue issue = (MutableIssue) event.issue

Suggest an answer

Log in or Sign up to answer