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

Edwin Chung May 8, 2014

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

0 votes
Answer accepted
Edwin Chung May 11, 2014

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 vote
Henning Tietgens
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.
May 8, 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 Sign up to answer