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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,556,191
Community Members
 
Community Events
184
Community Groups

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. 

2 answers

1 accepted

0 votes
Answer accepted
Lasse Langhorn
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 01, 2020

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

Lasse Langhorn
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 06, 2020

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!

Lasse Langhorn
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 09, 2020

Hi @Stephen Higgins

No problem.

Glad to help.

Regards

Lasse Langhorn

Hi

Has anyone adapted this script to Jira Cloud?

 

Thanks in davance

Hi

Has anyone adapted this script to Jira Cloud?

 

Thanks in advance

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events