Email Template

metzo4102 March 24, 2016

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

2 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.
March 24, 2016

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

 

 

metzo4102 March 24, 2016

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?

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.
April 21, 2016

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. 

0 votes
metzo4102 April 21, 2016

Worked perfectly!  Thanks!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events