Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×I am trying to send a bcc email via the "Send a custom email" script from scriptrunner but there is no option for bcc except for mail.setBcc. I am having trouble writing a script in the "Condition and Configuration" section to add a group to setBcc. This group would be sent an email once an external comment is made on an incident. I have provided my script so far which works if I use my own email but not with a group.
Any help or guidance is appreciated.
import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.comments.Comment
import groovy.json.JsonSlurper
final SD_PUBLIC_COMMENT = "sd.public.comment"
def event = event as IssueEvent
def user = event.getUser()
def comment = event.getComment()
def commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService)
def isExternal = { Comment c ->
def commentProperty = commentPropertyService.getProperty(user, c.id, SD_PUBLIC_COMMENT)
.getEntityProperty().getOrNull()
if (commentProperty) {
def props = new JsonSlurper().parseText(commentProperty.getValue())
!(props['internal'] as String).toBoolean()
}
else {
null
}
}
if (comment && issue.getIssueType().getName() == "Incident" && issue.priority?.name == '1') {
log.debug(comment)
mail.setBcc(GROUP GOES HERE?)
return isExternal(comment)
}
return false
Maybe you could get all users from the group and set users one by one?
ComponentAccessor.getUserUtil(). getAllUsersInGroupNames(Collection<Group>)
That is an example
import com.atlassian.jira.component.ComponentAccessor
def userList = ComponentAccessor.getUserUtil().getAllUsersInGroupNames(["jira-administrators"])
userList.each {
log.error(it.getEmailAddress())
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
import com.atlassian.jira.component.ComponentAccessor
def emailList
def userList = ComponentAccessor.getUserUtil().getAllUsersInGroupNames("IT Service Desk")
userList.each {
emailList = emailList + it.getEmailAddress().toString() + ","
}
mail.setTo(emailList)
I removed the brackets, not sure why they were there. Also now the script collects the email addresses in a string (you didn't add them up, just replaced the email).
I hope this makes sense. Let me know if that works.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Just in case someone needs this, I was able to make this work for me by changing two things @Krisztian Kovacs 's code (which was very helpful btw, thank you!).
1. I have to add back the brackets. Without brackets it's failing for me as getAllUsersInGroupNames is asking for a Collection.
def userList = ComponentAccessor.getUserUtil().getAllUsersInGroupNames(["IT Service Desk"])
2. Collecting email addresses - I just re-ordered the concatenation.
userList.each {
emailList = it.getEmailAddress().toString() + "," + emailList
}
What happened is during the declaration (def emailList), emailList's value is "null" which, using the original order of concatenation, was appended as "nullemailaddress@company.com, someotheremail@company.com,". The first email always be invalid - and in my case I was testing it on a group with just one member (me) to avoid flooding people, so it always seemed like it is not working at all. By reordering the concatenation and inserting the comma in between to isolate the "null" value did the trick for me.
As a help to newbies like me, in case you are wondering how I was able to "debug" the output of concatenation, I used the logging mechanism integrated in Scriptrunner - but I did not get the logging to work the fist time because of some default Jira configuration which was explain by @Alexey Matveev here:
https://community.atlassian.com/t5/Jira-questions/How-can-I-log-with-Script-Runner/qaq-p/775419
I opted for option 2.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This ended up not working. Close but what it is doing is adding the last user as the bcc address. When I tested it, I happen to be the last user which made me believe it was working.
@Krisztian Kovacs and/or @Alexey Matveev: any other suggestions?
Here is what the code looks like. Simplified it for testing.
import com.atlassian.jira.component.ComponentAccessor
def userList = ComponentAccessor.getUserUtil().getAllUsersInGroupNames(["IT Service Desk"])
userList.each {
mail.setTo(it.getEmailAddress())
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
mail.setBcc() expects a comma delimited string of email addresses, not group names.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'd go with @Alexey Matveev's solution, get all the email addresses in a group.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
wouldn't this just collect the usernames and not the email address? I am trying to find the documentation on Collection as I have never used it. Would it look like the one below? If so, it throws an exception that it can't find the class.
ComponentAccessor.getUserUtil(). getAllUsersInGroupNames(Collection<Inciden-Priority-1>)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
ComponentAccessor.getUserUtil(). getAllUsersInGroupNames(Inciden-Priority-1)
It'll give you a collection of usernames (list) and then go through each and add 'it.emailaddress' (if I remember correctly) to your bcc list.
I hope that makes sense.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Got it. Thank you @Krisztian Kovacs and @Alexey Matveev!
For anyone that is curious this is what it looks like
def recipient = ComponentAccessor.getUserUtil().getAllUsersInGroupNames(["Incident-Priority-1"])
recipient.each {
mail.setBcc(it.getEmailAddress())
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
awesome!
glad it works
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.