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 vote
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 Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Posted 13 hours ago in Jira

We want to know what Jira Service Desk apps you're using!

Hi Community! My name is Amir and I’m on the Jira Service Desk product marketing team at Atlassian. Our team would love to understand how you’re leveraging our ecosystem for Jira Service Desk. Wha...

36 views 0 5
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