ScriptRunner: set email address from custom field

I am trying to dynamically set the recipient addresses based on the settings of a custom field ('Notification List', checkbox collection contains a list of department)

In the condition & configuration section, I have a 

if ('Engineering' in cfValues['Notification List']*.value) {
recipients = recipients + ",addr1@host.com"
}
if ('Purchasing' in cfValues['Notification List']*.value) {
recipients = recipients + ",addr2@host.com"
}
mail.setTo(recipients)

So far so good, and working as expected

However, I'd like to pull from another custom field (ProjMgr) for one of the conditions

if ('Project Management' in cfValues['Notification List']*.value) {
    pm = com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("ProjMgr")
    pmValue = issue.getCustomFieldValue(pm)
    recipients = recipients + "," + pmValue
}

ProjMgr is User Picker type.  In this case, I got the user id in pmvalue.  How can I find the user's email address to construct the recipients list?

 

2 answers

Maybe you try the following:

ger().getCustomFieldObjectByName("ProjMgr")

...
    pmValue = issue.getCustomFieldValue(pm)
	if (pmValue != null && !((String) pmVavlue).isEmpty())
	{
		def user = ComponentAccessor.getUserManager().getUserByKey((String) pmValue);
		def mail = user.getEmailAddress() 
	}
 .....

https://docs.atlassian.com/jira/7.1.7/com/atlassian/jira/component/ComponentAccessor.html

https://docs.atlassian.com/jira/7.1.7/com/atlassian/jira/user/util/UserManager.html

https://docs.atlassian.com/jira/7.1.7/com/atlassian/jira/user/ApplicationUser.html

 

Hope this helps!

 

Cheers,

Julian

Thanks Julian.

 

I got this error "Cannot invoke method getEmailAddress() on null object" when I tried to preview my codes. 

What does: "(String) pmValue " produce? Add some logging for that variable.

Basically no user can be found with that key.

pmValue returns "jackson.loong(jackson.loong)"
and
user returns null

 

I ended up using the following codes segment and seems to work

if ('Project Management' in cfValues['Notification List']*.value) {
    def pm = customFieldManager.getCustomFieldObjectByName("ProjMgr")
    if (pm != null) {
        def pmValue = issue.getCustomFieldValue(pm)
        if (pmValue != null && !((String) pmValue).isEmpty()) {
            def mailbox = ((ApplicationUser) pmValue).getEmailAddress() 
            recipients = recipients + "," + mailbox            
        }
     }
}
0 vote

pmValue in your emample above should actually be an ApplicationUser object (assuming JIRA 7).

So the email address would be 

pmValue.emailAddress



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

89 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