How to share issue by email using ScriptRunner?

I know JIRA has API method for share issue by email. And I have custom script in post-function. I want to send this email from my script. Built-in script Send Custom Email doesn't fit for me because I need send email just like if I pressed Share button on Issue screen. API doesn't fit too.

I dont know other ways for it.

1 answer

1 accepted

1 vote
Accepted answer

What's wrong with the Send Custom Email? It's not clear why it's not sufficient.

Code example

First of all, the Share feature is provided by a plugin, so we need to use the @WithPlugin and @PluginModule annotations instead of just importing the classes.

Second, we must construct a ShareBean, which represents most of the input for the operation. Then, we need to pass the bean, the Issue, and the Sender (user) to ShareService.validateShareIssue() method.

Finally, this returns a Validation Result. The result is either Valid or has Errors. We check the state and either log an error, or proceed and share the issue.

import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.plugins.share.ShareService
import com.atlassian.jira.plugins.share.ShareBean

import java.util.Collections


ShareService shareService

def remoteUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def user = ComponentAccessor.userManager.getUserByKey("admin")
def issue = ComponentAccessor.issueManager.getIssueByCurrentKey("PROJ-1")

def userkeys = Collections.singleton(user.key) as Set<String>
def emails = Collections.emptySet() as Set<String>
def message = "Hello World!" as String

def shareBean = new ShareBean(
null // jql

def result = shareService.validateShareIssue(remoteUser, shareBean, issue)

if(!result.isValid()) {
log.error "Failed to validate Share Issue parameters:\n${result.errorCollection}"
return null
} "${remoteUser.displayName} is sharing ${issue.key} ${issue.summary} with ${user.displayName}."


Because I need exactely same email as if I pressed Share buttom. Email must be autogenerated by JIRA and must have appropriate styles. Just like in the picture:


Steven Behnke Community Champion Jan 11, 2018

Then just construct your own template that looks like it. You can copy it from the emails you get.

So.. There is no way to do it with standard JIRA tools?

Steven Behnke Community Champion Jan 11, 2018

Very few people have tried to do what you're doing.

Your code example works fine, thakn you!

Steven Behnke Community Champion Jan 11, 2018

Oh excellent! :) I hadn't ever seen those classes or API before, so I wasn't really sure... I did a single test and didn't see it work... I guess I'll have to look closer! 

Glad it worked though, and thanks for the update!

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Jira

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,084 views 0 8
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