Order of execution for ScriptRunner Listeners

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

This widget could not be displayed.

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

528 views 3 19
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you