Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to Create a ScriptRunner Job to email when filter results = 0

I am trying to find a way to create a scheduled ScriptRunner job that will send a customized email notification to a JIRA group when the results from a shared filter return 0 rows.  I tried just using the native JIRA filter subscriptions, but there isn't an option to ONLY send when 0 results. Any help would be appreciated. 

1 answer

1 accepted

0 votes
Answer accepted

Hi,

If you have Scriptrunner installed you can go to Jira Administration > Manage apps > Scriptrunner > Jobs > Click Create jobs > Click Custom  scheduled job.

  • Write a note
  • Choose the script user
  • Enter the cron expression that you want to use
  • Modify the script below to suit your needs (filterId, sendToEmail and jiraGroups)

Have not tried the script below but I think it will get you an idea.

Pseudo Groovy code below:

package dk.langhornweb

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchRequestManager
import com.atlassian.jira.mail.Email
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.mail.server.SMTPMailServer

def filterId = 10L
def sendToEmail = "to-email@mail.com"
def jiraGroups = ["jira-software-users"]

def searchService = ComponentAccessor.getComponent(SearchService)
def jiraAuthContext = ComponentAccessor.getJiraAuthenticationContext()
def searchRequestManager = ComponentAccessor.getComponent(SearchRequestManager.class)
def searchRequest = searchRequestManager.getSearchRequestById(filterId)
def user = jiraAuthContext.getLoggedInUser()

def searchResult = searchService.search(user, searchRequest.query, PagerFilter.unlimitedFilter)
if (searchResult.issues.size() == 0) {
def bcc = buildBcc(sendToEmail, jiraGroups)
sendEmail(sendToEmail, bcc, "Jira filter size is 0", "This is the mail body")
}

// Build email bcc
def buildBcc(defaultEmail, groups) {
def bcc = defaultEmail
def groupManager = ComponentAccessor.getGroupManager()
def i = 0
while ( i < groups.size()) {
def members = groupManager.getUsersInGroup(groups[i])
def j = 0
while ( j < members.size()) {
bcc = bcc + "," + members[j].getEmailAddress()
j++
}
i++
}
return bcc
}

// Create an email and send
def sendEmail(String emailAddr, String bcc, String subject, String body) {
SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
if (mailServer) {
Email email = new Email(emailAddr)
email.setBcc(bcc)
email.setSubject(subject)
email.setBody(body)
mailServer.send(email)
log.debug("Mail sent")
} else {
log.warn("Please make sure that a valid mailServer is configured")
}
}

Regards

Lasse Langhorn

Thank you for your help Lasse. I am receiving the following errors when trying to setup the job as you have noted. Not sure if I am missing something fundamental here. 

emailScriptError01.JPG

Hi @Stephen Higgins

I have created an improved version of the script.

package dk.langhornweb

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchRequestManager
import com.atlassian.jira.mail.Email
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.mail.server.SMTPMailServer

def filterId = 10L
def sendToEmail = "to-email@mail.com"
def jiraGroups = ["jira-software-users"]

def searchService = ComponentAccessor.getComponent(SearchService)
def jiraAuthContext = ComponentAccessor.getJiraAuthenticationContext()
def searchRequestManager = ComponentAccessor.getComponent(SearchRequestManager.class)
def searchRequest = searchRequestManager.getSearchRequestById(filterId)
def user = jiraAuthContext.getLoggedInUser()

def searchResult = searchService.search(user, searchRequest.query, PagerFilter.unlimitedFilter)
if (searchResult.results.size() == 0) {
String bcc = buildBcc(sendToEmail, jiraGroups)
sendEmail(sendToEmail, bcc, "Jira filter size is 0", "This is the mail body")
}

// Build email bcc
def buildBcc(def defaultEmail, List<String> groups) {
def bccList = [defaultEmail]
def groupManager = ComponentAccessor.getGroupManager()
groups.each { group ->
Collection<ApplicationUser> memberList = groupManager.getUsersInGroup(group)
memberList.each { member ->
bccList.add(member.getEmailAddress())
}
}
return bccList.join(",")
}

// Create an email and send
def sendEmail(def emailAddr, def bcc, def subject, def body) {
SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
if (mailServer) {
Email email = new Email(emailAddr as String)
email.setBcc(bcc as String)
email.setSubject(subject as String)
email.setBody(body as String)
mailServer.send(email)
log.debug("Mail sent")
} else {
log.warn("Please make sure that a valid mailServer is configured")
}
}

Hope this will work.

Regards

Lasse Langhorn

@Lasse Langhorn it works like a charm! Thank you for your help on this item!

Hi @Stephen Higgins

No problem.

Glad to help.

Regards

Lasse Langhorn

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Opsgenie

Investigate deployment-related incidents with Bamboo and Opsgenie together

Back in April of last year one of the major product announcements from Opsgenie was the launch of the Incident investigation view which created a deep connection between Bitbucket and Opsgenie, empow...

149 views 1 4
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you