ScriptRunner: Send Email to User from User-Picker CF

dD January 22, 2022

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

5 votes
Answer accepted
Vishwas
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.
January 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

dD January 22, 2022

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
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.
January 22, 2022

That's great !!

Thanks @dD 

Regards,

Vishwas

0 votes
Drishti Maharaj May 10, 2022

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