Forums

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

send notification to users

RichardA June 26, 2020

Hi Team,

my requirement is need to send the notification mail to all users available in 1,2 & 3 multi user picker fields and we need to trigger the mail daily

Kindly let me know any solution for this.

Thanks.

3 answers

0 votes
RichardA July 9, 2020

Hi @Ivan Tovbin , Sorry for the delay, now am trying to resolve this issue using Jobs option in script runner Plugin.

Ivan Tovbin
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.
July 9, 2020

@RichardA  

It seems the problem is in this line:

def id = ("Email Address: "+userSelected.getEmailAddress());

First off, the value doesn't really make much sense. Secondly if your want to use this variable outside of your cycle then you need to declare it outside first.

RichardA July 9, 2020

Hi @Ivan Tovbin , Can you help to get the solution for this issue?

I tried to resolve this problem with so many ways but i couldn't found the exact template to get this. Thanks

Ivan Tovbin
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.
July 9, 2020

Try the code I provided above, should do the trick

RichardA July 9, 2020

Hi @Ivan Tovbin , Now i have used your code which is provided above. but still it's not working. there are no errors in our code still emails are not getting triggered to users and got some error in log.

 

Ivan Tovbin
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.
July 10, 2020

Well that's because 'issues' variable is not declared in your script. As is 'emails' by the way. It looks like you took only a part of my code and expect it to work like this. It won't.

What you should do is configure a scripted job and make it run the code I provided in its entirety. Check this documentation for more info.

RichardA July 10, 2020

Hi @Ivan Tovbin , Thanks for the replay.

just i have configure a new scripted job still showing some errors.

 

 

RichardA July 13, 2020

Hi @Ivan Tovbin , can you help to get the solution for this? Thanks

0 votes
Ivan Tovbin
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.
June 27, 2020

A scripted service seems to be the solution here. Here's the logic:

1) Create a service that runs once at the start of each day (say 0005 am).

2) Make it run a JQL search using this query:

"Approver Date" >= startOfDay() AND "Approver Date" <= startOfDay(7)

This way it should only return issues which have less than one week remaining on their Approver Date.

3) If any such issues are found, collect the users from their respective fields and send them a custom notification. 

RichardA June 28, 2020

Hi @Ivan Tovbin 

thanks for your response :)

how can i create a service in jira. can you more elaborate on this?

can we know the logic in B/W services and JQL. and how this logic will trigger the custom mails to users in a multi-picker field.

Ivan Tovbin
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.
June 28, 2020

Please advise the version of Jira Server and Scriptrunner that you are using.

RichardA June 28, 2020

version of jira server : v8.5.4 & script runner version : 5.8.0-p5 Thanks.

RichardA July 6, 2020

Hi @Ivan Tovbin ,

can you help to get the solution for this? Thanks.

Ivan Tovbin
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.
July 6, 2020

Hi @RichardA 

Sorry for the delay. The code for your service should look something like this:

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.query.Query
import com.atlassian.mail.Email
import javax.mail.Message

ApplicationUser user = ComponentAccessor.getUserManager().getUserByName("userName")
String jql = "\"Approver Date\" >= startOfDay() AND \"Approver Date\" <= startOfDay(7)"
List<MutableIssue> issues = searchIssuesByJql(jql, user)

if (issues) {
List<String> fieldNames = ["Approver", "Manager", "Editor"]
List<CustomField> customFields = fieldNames.collect { ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(it) }
issues.each { issue ->
List<String> emails
customFields.each { customField ->
emails = emails + issue.getCustomFieldValue(customField).collect { it.getEmailAddress() }
}
if (emails) {
emails.each { email ->
String subject = "Some subject"
String body = "Some body"
sendEmail(email, subject, body)
}
}
}
}

List<MutableIssue> searchIssuesByJql(String jql, ApplicationUser searchingUser) {
JqlQueryParser jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
SearchService searchService = ComponentAccessor.getComponent(SearchService)
Query query = jqlQueryParser.parseQuery(jql)
SearchResults results = searchService.search(user, query, PagerFilter.getUnlimitedFilter())
return results.getResults()
}

void sendEmail(String to, String subject, String body) {
Email email = new Email()
email.setTo(to)
email.setSubject(subject)
email.setBody(body)
SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
Thread.currentThread().setContextClassLoader(Message.class.getClassLoader())
mailServer.send(email)
}

 As you can see, you need to specify the username which will be used to run your jql search. You might also want to modify that jql query to narrow your search down as much as possible. And I suppose you could also modify the code so it sends one email to all concerned users at once, instead of sending each user a separate email, like it does now.

Like RichardA likes this
0 votes
Rudy Holtkamp
Community Champion
June 26, 2020

This could be easily done with Automation for Jira, however looking at the tags you use scriptrunner. If that is your daily driver, somebody else has to answer this. If you are also using / willing to use Automation for Jira, then I'm happy to help. 

Suggest an answer

Log in or Sign up to answer