Script Runner, can I use a Groovy Script in the "To" (Email fields?

Currently, we're using Script Runner Listeners to send emails on certain events. We have 15 of these created -- but they just handle different versions of the same two events.

We would be able to get this down to 2 listeners if I could do the following:

To addresses (or To issue fields):

<% out << (issue.priority.name == "P1 - Critical" ? "role:cellphonepager" : "role:projectlead") %> or

<% if(issue.priority.name == "P1 - Critical") { out << "role:cellphonepager" } else {out << "role:projectlead"} %>

We already use a lot of these for Subjects and the email template, but it didn't work when we tried to use this to address the email. Any suggestions how to accomplish?

Thanks, -Ed

2 answers

1 accepted

Henning, that was a perfect suggestion. Thank you!

Inside of our Script Runner Event Listener, we set the 'To issue fields' to 'customfield_10700'. Custom Field ID 10700 is a scripted field:

//THIS CUSTOM FIELD CONTAINS THE TARGETS FOR OUTGOING SMS ALERTING:
//P1 -&gt; oncall all the time
//P1 -&gt; primary-secondary during biz hours
//P2 -&gt; primary-secondary during biz hours
//P3, P4 -&gt; no SMS sent
//ESCALATED -&gt; escalatedpager@spkaa.com

import com.atlassian.jira.project.Project
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.security.roles.*
Date now = new Date()
//create empty notifyList string
def notifyList = ""
//Create new objects used for determining ticket project and roles and roleActors (users in role)
Project myProject = issue.getProjectObject()
ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager cfManager = componentManager.getCustomFieldManager()
CustomField cf = cfManager.getCustomFieldObject(10207) //Escalation Status custom field

ProjectRoleManager roleManager = componentManager.getComponent(ProjectRoleManager.class)
ProjectRole role = roleManager.getProjectRole("Primary-Secondary-Email")
ProjectRoleActors actors = roleManager.getProjectRoleActors(role, myProject)

//For a P1 during business hours, add Primary-Secondary-Pager and ONCALL
if ((now.hours &gt;= 9) &amp;&amp; (now.hours &lt; 18) &amp;&amp; issue.priority.name.contains("P1")) {
    role = roleManager.getProjectRole("Primary-Secondary-Pager")
    actors = roleManager.getProjectRoleActors(role, myProject)
    actors.getUsers().each { i -&gt;
            notifyList += i.getEmailAddress() + " "
            }
    notifyList += "emergencypager@spkaa.com "
}

//For a P1 AFTER business hours, add ONCALL
if ((now.hours &lt; 9) &amp;&amp; (now.hours &gt;= 18) &amp;&amp; issue.priority.name.contains("P1")) {
    notifyList += "emergencypager@spkaa.com "
}

//For a P2 during business hours, add Primary-Secondary-Pager.
if ((now.hours &gt;= 9) &amp;&amp; (now.hours &lt; 18) &amp;&amp; issue.priority.name.contains("P2")) {
    role = roleManager.getProjectRole("Primary-Secondary-Pager")
    actors = roleManager.getProjectRoleActors(role, myProject)
    actors.getUsers().each { i -&gt;
            notifyList += i.getEmailAddress() + " "
            }
}

//If the ticket is ESCALATED, include escalate@spkaa.com
if (issue.getCustomFieldValue(cf).toString() == "Escalated") {
    notifyList += "escalatedpager@spkaa.com "
}

return notifyList

We use this to ensure that the P1 and P2 helpdesk tickets notify the correct people - and limit the notifications after business-hours. We also use this with the Escalate Ticket built-in script as part of our "Escalation System" in case anything gets missed.

The //For a P2 during... block is a good example of getting all email addresses for a ticket's project role named "Primary-Secondary-Pager", in case someone is looking for just that piece.

1 votes
Henning Tietgens Community Champion May 08, 2014

Maybe you could create a scripted custom field which "calculates" to correct receiver for the issue and than use this field as "To:".

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,147 views 13 19
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot