ScriptRunner Bcc Group Email

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

4 answers

2 votes
Alexey Matveev Community Champion Jan 23, 2018

Maybe you could get all users from the group and set users one by one?

ComponentAccessor.getUserUtil(). getAllUsersInGroupNames(Collection<Group>)

Alexey Matveev Community Champion Jan 24, 2018

That is an example

import com.atlassian.jira.component.ComponentAccessor

def userList = ComponentAccessor.getUserUtil().getAllUsersInGroupNames(["jira-administrators"])
userList.each {
log.error(it.getEmailAddress())
}
0 vote
Steven Behnke Community Champion Jan 23, 2018

mail.setBcc() expects a comma delimited string of email addresses, not group names.

I am reading that now. Any alternative suggestions on this? 

Krisz [Adaptavist] Community Champion Jan 24, 2018

I'd go with @Alexey Matveev's solution, get all the email addresses in a group.

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>)

Krisz [Adaptavist] Community Champion Jan 24, 2018

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. 

Got it. Thank you @Krisz [Adaptavist] 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())
}

Krisz [Adaptavist] Community Champion Jan 24, 2018

awesome!

glad it works

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. 

@Krisz [Adaptavist] 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())
}
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. 

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

70 views 0 5
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you