Forums

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

ScriptRunner - problem in Script Listener

Amir Katz (Outseer)
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.
May 22, 2018

I need to force a transition when an issue is assigned (via the Assign button), but only if it was previously unassigned. Following the documentation about Fast-track transition an issue,

https://scriptrunner.adaptavist.com/latest/jira/builtin-scripts.html#_fast_track_transition_an_issue

Then building on the example given in the Script Listeners console, I created a listener on the Issue Assigned event, on my project, using this condition:

import com.onresolve.scriptrunner.runner.util.UserMessageUtil

UserMessageUtil.info('Checking condition for fast-track...')
UserMessageUtil.info("i.type=" + issue.issueType.name)
UserMessageUtil.info("issue.new_assignee=" + issue.assignee + ", old.assignee=" + originalIssue.assignee)

// Our issue types + Assignee changed but only if previously was unassigned
def result = (issue.issueType.name in ["FRI Incident", "FRI Service Request"] && (issue.assignee != originalIssue.assignee) && (originalIssue.assignee == null))

UserMessageUtil.info("Result: '" + result + "'")
return result

However, when I open an unassigned issue (which matches the issue type condition) and assign it, the listener is triggered, but the condition always returns false because the value of originlaIssue.assignee apparently is set to the new assignee - see screenshot.

Seems like a bug in the implementation, or a bug in the example.

Anyone?

Listener-01.png

2 answers

1 accepted

1 vote
Answer accepted
Roland Holban (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.
May 22, 2018

This condition will return true if previous user was Unassigned and current user is not Unassigned. You can add code to check for issue type as well

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean

def changeHistoryManager = ComponentAccessor.changeHistoryManager
def assignees = changeHistoryManager.getChangeItemsForField(event.issue, "assignee")

// Sort assignees by date
Collections.sort(assignees, new Comparator<ChangeItemBean>() {
int compare(ChangeItemBean o1, ChangeItemBean o2) {
return o1.getCreated() <=> o2.getCreated()
}
})

def currentUser = assignees[-1].to
def previousUser = assignees[-1].from

currentUser != null && previousUser == null
0 votes
Amir Katz (Outseer)
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.
May 22, 2018

I've just noticed that the example that I used from the Script Listeners console (Assignee Changed) is tagged as Workflow functions only

I assume that's the reason why my code does not work as a condition for a stand-alone listener. 

I am now using Roland's answer above and it works.

Suggest an answer

Log in or Sign up to answer