Email Template

I'm having script runner send an email.  I want the email to list and provide links to any issues linked to the JIRA that is running the script.  The query to find all issues linked is 'issueFunction in linkedIssuesOf("issue = <issue>") '  However, if I put that in the template, it just spits that out in plain text and doesn't actually query for results.  I'm not experienced in email templates unfortunately.  How would I make that query executable?

2 answers

1 accepted

Hi Jeff

So, if I understand well you want to send an email (either via a listener or a post function) with all the linked issues to the 'original' issue. Below is a script that concat in a string all the linked issues (and not the subtasks like the jql does).

You need to use the "currentUser" binding when your in a post-function, as looking it up from the authentication context returns null.

&lt;%
//code goes here
def issueLinkManager = com.atlassian.jira.component.ComponentAccessor.getIssueLinkManager()
def linkCollection = issueLinkManager?.getLinkCollection(issue, currentUser)
//collect all issues in a list
def allLinkedIssues = linkCollection?.getAllIssues()
//concat all issues links in a  string
def linkedIssues = allLinkedIssues.collect {issue -&gt;
    '&lt;a href="&lt;yourBaseUrl&gt;/jira/browse/' + issue.key + '"&gt;' + issue.key + '&lt;/a&gt;'
}.join("&lt;br&gt;")
%&gt;
Linked issues : &lt;br&gt; $linkedIssues

 If your email format is HTML you will get something like that (Tested in JIRA v6.4.7)

preview-screenshot.png

If you however you want to go with the jql search

&lt;%
//code goes here
def issueManager = com.atlassian.jira.component.ComponentAccessor.getIssueManager()
def searchService = com.atlassian.jira.component.ComponentAccessor.getComponent(
com.atlassian.jira.bc.issue.search.SearchService)
//jql search for the current issue key
def jqlSearch = 'issueFunction in linkedIssuesOf("issue = ' + issue.key + '")'
def parseResult =  searchService.parseQuery(currentUser, jqlSearch)
def allLinkedIssues = []
if (parseResult.isValid()) {
    def searchResult = searchService.search(currentUser, parseResult.getQuery(), 
    com.atlassian.jira.web.bean.PagerFilter.getUnlimitedFilter())
    allLinkedIssues = searchResult.issues.collect { issueManager.getIssueObject(it.id) }
}
//collect all issues in a list
//concat all issues links in a  string
def linkedIssues = "&lt;br&gt;"
for (def issue in allLinkedIssues) {
	linkedIssues = linkedIssues + 
    '&lt;a href="&lt;yourBaseUrl&gt;/jira/browse/' + issue.key + '"&gt;' + issue.key + '&lt;/a&gt;&lt;br&gt;' 
}
%&gt;
Linked issues : $linkedIssues

 

 

Hi Thanos!  Yes, that is exactly what I'm trying to do.  However, I can't seem to get it working.  I'm running 6.4 currently.  Here's what the email template looks like in my post function.

&lt;%
//code goes here
def User = com.atlassian.jira.component.ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueLinkManager = com.atlassian.jira.component.ComponentAccessor.getIssueLinkManager()
def linkCollection = issueLinkManager?.getLinkCollection(issue, User)
//collect all issues in a list
def allLinkedIssues = linkCollection?.getAllIssues()
//concat all issues links in a  string
def linkedIssues = "&lt;br&gt;"
for (def issue in allLinkedIssues) {
    linkedIssues = linkedIssues +
    '&lt;a href="tracker.redbrickhealth.local/jira/browse/' + issue.key + '"&gt;' + issue.key + '&lt;/a&gt;&lt;br&gt;'
}
%&gt;
Linked issues : $linkedIssues

The email I receive just shows

Linked issues : 

 

There are no linked issues like in your example above, but that is exactly what I want.  Did I change the user sections correctly?

Hey Jeff,

Apologies for the delay. A colleague @Adam Markham [Adaptavist] pointed out thet instead of the  def User = com.atlassian.jira.component.ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() you should use the currentUser

I updated the original answer above. 

Worked perfectly!  Thanks!

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

136 views 0 4
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