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,300,062
Community Members
 
Community Events
165
Community Groups

ScriptRunner: Send Email to User from User-Picker CF

Hey folks I need your help, I guess it is a simple question.

I use Scriptrunner to send emails to assignees and this works well. I send the email with this line of code:

sendEmail(emailAddr, emailSubject, emailBody)

But know I dont want to send it to the assignee (assignee.getEmailAddress()), I want to send it to a user, that is selected via a customfield, which is a user picker.

When I write:

def user = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(10414))
emailAddress = user.getEmailAddress() 

I receive an Error because jira things of it as an object and not as an user. how can I change that?

 

Best  

2 answers

1 accepted

4 votes
Answer accepted
Vishwas Community Leader Jan 22, 2022

Hey @dD 

You may have to try the code like this which i pulled out from here https://community.atlassian.com/t5/Jira-Software-questions/Scriptrunner-Script-Field-Custom-Email/qaq-p/1749864

import com.atlassian.mail.Email
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

def customFieldManager = ComponentAccessor.customFieldManager

def mailServerManager = ComponentAccessor.mailServerManager

def mailServer = mailServerManager.defaultSMTPMailServer

def users = customFieldManager.getCustomFieldObjectsByName("<custom_field_Name>")[0]

def usersSet = issue.getCustomFieldValue(users) as Set<ApplicationUser>

usersSet.each {
def emailAddress = it.emailAddress
def subject = "Test"
def body = "This is a test message"
def email = new Email(emailAddress)
email.setSubject(subject)
email.setBody(body)
email.setMimeType("text/html")
def threadClassLoader = Thread.currentThread().contextClassLoader
Thread.currentThread().contextClassLoader = mailServer.class.classLoader
mailServer.send(email)
Thread.currentThread().contextClassLoader = threadClassLoader
}

 Hope this helps !!

 

Regards,

Vishwas

Thank you @Vishwas .

 

Actually I just figured out that I can ignore the error message in my script - it works.

I think your script will also work.

Best

Vishwas Community Leader Jan 22, 2022

That's great !!

Thanks @dD 

Regards,

Vishwas

Hi @dD - I cam across your post and I am actually facing a very similar problem. Sending an email to a user picked in a custom field - how did you manage to resolve it as I am also getting an error and my code does not run because of that? Thanks.

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you