Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Send custom email Using ScriptRunner based on Filter Condition results

Nagappan Murugappan February 16, 2021

Hello

 

I am completely new to JIRA administration, we are using script runner to schedule different jobs, and one of the requirement is, based on the certain filter condition results, a job needs to be scheduled on certain time frame to send email with the customized body, JIRA ticket number etc,. 

What i have tested so far is just simple email sending code which is as follows..

 

 

import com.atlassian.mail.Email
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.jira.component.ComponentAccessor

def emailSubject = "JIRA CHECK"
def emailBody = "Test CHECK"
def emailAddress = "<emaild id>"


SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()


if (mailServer) {
Email email = new Email(emailAddress)
email.setSubject(emailSubject)
email.setBody(emailBody)
mailServer.send(email)
} else {}

4 answers

1 accepted

0 votes
Answer accepted
Nagappan Murugappan February 22, 2021

Hello Helmy

 

Sorry about the late reply, i would try your option and let you know whether it helps. 

I already have an flter_id eg 123, i want to see whether 123 returns me any result, if yes, then with the content of the Customized email message, i want to send to the respective recipients.

 

Thank You.

1 vote
Helmy Ibrahim _Adaptavist_
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.
February 16, 2021

Hi Nagppan,

Thank you for using ScriptRunner.

Can you clarify what you meant by "Filter Condition"? Providing examples would help too!

As for the scheduled script, you could use Script Jobs by ScriptRunner. It accepts cron expression to define the interval a job is run at.

Nagappan Murugappan February 17, 2021

Hi Helmy

 

For example 

issuetype = Issue AND status in ("In Progress", Open) and created < -10w

 

I want to scan the results from the above filer (have saved the filter too and have the filter id with me), and if the filter returns any result, then would like to sent Email to certain email id.

Helmy Ibrahim _Adaptavist_
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.
February 19, 2021

Hi Nagppan,

You can execute a JQL query in your code and then do a simple checking:

if (search.results) {
// send email
}

I hope this helps!

0 votes
Nagappan Murugappan February 23, 2021

Hello Helmy

 

Thanks for the link, it really helped and i was able to send the custom email. 

Vladislav
Contributor
March 31, 2021

Hello @Nagappan Murugappan

Would you please be able to share your script?

Thank you in advance!

Nagappan Murugappan April 13, 2021

@Vladislav sorry for the late reply, i wasnt working few weeks now. Please find the script below. 

 

import groovy.sql.Sql
import java.sql.Driver
import com.atlassian.mail.Email
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.web.bean.PagerFilter
import org.ofbiz.core.entity.ConnectionFactory
import org.ofbiz.core.entity.DelegatorInterface
import java.sql.Connection
import java.util.Date
import java.time.*
import java.lang.String
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.text.ParseException;

def delegator = (DelegatorInterface) ComponentAccessor.getComponent(DelegatorInterface)
String helperName = delegator.getGroupHelperName("default")

def issueManager = ComponentAccessor.getIssueManager()
def searchService = ComponentAccessor.getComponent(SearchService)
def jqlSearch = "issuetype = Issue AND status in ('In Progress', 'Closed') AND (created < -7w AND priority = Medium OR created < -2w AND priority = High OR created < -0.5w AND priority = Highest) ORDER BY priority, status DESC"
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueKeyIns
def SummaryIns
def prioIns
def createdIns
def emailIns
def idIns
def updateCount
def assigneeEmail
def assigneeId


// eventually this array will contain all the issues that returned from the JQL
def issues = []
// hashmap to store username and associated issue(s)
def map = [:].withDefault {
[]
} //The default value will be created every time a non-existing key is accessed.
// Need this to be able to dynamically expand/update map
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)

 

 


if (parseResult.isValid()) {
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues = searchResult.getResults()
//log.warn(issues)
}


// breaking out the issues per assignee. Will use later when we email the assignees
issues.each {
def assignee = it.assigneeId
map[assignee].add(it)
}

// following section creates separate emails for each assignee from the map, including all related issues in the same email.
map.each { key, value ->
def assignee = key
def theirIssues = value as List



String cc = ""
String bcc = ""

theirIssues.each {
assigneeEmail = ("<to email id here>")
idIns = it.id
issueKeyIns = it.key
SummaryIns = it.getSummary()
prioIns = it.getPriorityObject().getId()
createdIns = it.getCreated()
emailIns = it.getAssignee().getEmailAddress()
assigneeId = it.getAssignee().getName()
TimeZone.getTimeZone('UTC')
def body2 = "Email body"

sendEmail(assigneeEmail, cc, bcc, SummaryIns, body2,issueKeyIns)

}

}

// Create an email method
def sendEmail(String emailAddr, String cc, String bcc, String subjectIns, String body,String issueKeyIns )
{
def mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
if (mailServer) {
def email = new Email(emailAddr)
email.setMimeType("text/html")
email.setFrom ("<from email id here>")
email.setSubject(subjectIns)
email.setBody(body)
email.setCc(cc)
email.setBcc(bcc)
mailServer.send(email)
log.warn("Email sent")

} else {
log.warn("Please make sure that a valid mailServer is configured")
}


}

Like Vladislav likes this
Vladislav
Contributor
April 14, 2021

@Nagappan Murugappan 

Thank you very much! I really appreciate it!

0 votes
Boris Berenberg - Atlas Authority
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.
February 16, 2021

Hi Nagappan,

We don't yet support customized bodies, but we support everything else you said out of the box with Notification Assistant for Jira (a Staff Pick).  And we have customized notification templates coming in the next month! 

Suggest an answer

Log in or Sign up to answer