Order of execution for ScriptRunner Listeners

Scott Evans August 10, 2017

I have 2 custom script Listeners. One Listener will create a custom ID for a JIRA issue. The other Listener will update another system (AccuRev) with information from the JIRA issue. I want both of these to run on Issue Created events but I want to make sure the ID Listener runs before the AccuRev Listener runs. 

I am using JIRA 7.3.6. When I go to the Script Listeners pane, I have the ID Listener before the AccuRev Listener. Is that order related to any execution order?

George

1 answer

1 accepted

0 votes
Answer accepted
Thanos Batagiannis _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 13, 2017

i George, 

You cannot set the order that the listeners will get executed which means if you have both your listeners to listen for the issue created you will never be sure which of them will run first.

What you can do tho is to have a listener that will listen for the issue created event - I beleive in your case this will be the one that will create a custom ID for JIRA and then your AccuRev listener that will listen for a custom event.

So the question now is how to trigger the custom event you created. The solution is, in the script you have for the create a custom ID for JIRA listener to programmatically trigger the custom event. 

This is how the script for the create a custom ID for JIRA listener will look like 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.issue.IssueEventBundle
import com.atlassian.jira.event.issue.IssueEventBundleFactory
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.user.ApplicationUser

// Listener that listens to an Issue Created event

def issue = event.issue
def asUser = ComponentAccessor.getUserManager().getUserByKey("admin")

log.debug "Listener that do something with the ID triggered"

// code here that do something with the id

// get the id of custom the event you want to trigger
def eventId = ComponentAccessor.eventTypeManager.eventTypes.find {it.name == "Issue Id Created"}?.id

// call the method that will raise the given event, so it will be caught by the next listener
triggerEvent(issue, asUser, eventId)

/**
* A method that raises an event
* @param issue The issue that the event will raise for
* @param user The user that the event will raised from
* @param eventId The event id to raise
*/
def triggerEvent (Issue issue, ApplicationUser user, Long eventId) {
def issueEventManager = ComponentAccessor.getIssueEventManager()
def issueEventFactory = ComponentAccessor.getComponent(IssueEventBundleFactory)

def eventBundle = issueEventFactory.wrapInBundle(new IssueEvent (issue, null, user, eventId, true)) as IssueEventBundle
issueEventManager.dispatchEvent(eventBundle)
}

And then you will have the listener that will listens to  the custom event that raised form the previous script....

Hope that makes sense, shout if you need further assistance ...

Kind regards, 

Thanos

Alvin
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.
January 9, 2020

Hi Thanos,

Good day! I have the same scenario like this, but my 1st listener is a custom listener, and my 2nd listener is send an email listener. Is this still applicable?

Best Regards,
Alvin

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events