Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,559,306
Community Members
 
Community Events
184
Community Groups

trying get user from a customfield user picker using groovy script

I am trying to get the user from customfield using groovy but its not working .Please help

Issue issue
UserUtil userUtil = ComponentAccessor.userUtil
def customField = ComponentAccessor.getComponent(CustomFieldManager).getCustomFieldObject("customfield_12202")
def userName = customField.getValueFromIssue(issue)
def user = userUtil.getUserByName(userName).getDirectoryUser()
log.info "check this ${user}\n"

Where am i making the mistake ?

2 answers

Hi, 

Just sharing how I was retrieving the user information. I did by just type-casting with ApplicationUser. Please refer below code sample for both single-select and multi-select user picker.

 

//When customfield_XXXXX is single select user-picker
CustomField cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_XXXXX");
def cfVal = issue.getCustomFieldValue(cf);
if(cfVal){
ApplicationUser userSelected = (ApplicationUser) cfVal;
if(userSelected){
log.warn("Username: "+userSelected.getUsername());
log.warn("Email Address: "+userSelected.getEmailAddress());
}
}

//When customfield_XXXXX is multi select user-picker
CustomField cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_XXXXX");
def cfValList = issue.getCustomFieldValue(cf);
if(cfValList){
for(cfVal in cfValList){
ApplicationUser userSelected = (ApplicationUser) cfVal;
if(userSelected){
log.warn("Username: "+userSelected.getUsername());
log.warn("Email Address: "+userSelected.getEmailAddress());
}
}
}

Hi @saravp2020 , i have similar kind of requirement. need to send the notification mail to all users available in A and B user fields.

 

Hi @RichardA

I have not tried using EmailService API, but in your code, it is declared and used directly without configuring any SMTP server, that could be the reason, of mail not triggered.

Please try below piece of code, we use this for sending one mail at a time, and loop it for more mails.

import com.atlassian.mail.Email;
import com.atlassian.mail.server.SMTPMailServer;
import com.atlassian.jira.component.ComponentAccessor;

def emailId = "abc@xyz.com";
SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer();
if(mailServer){
Email email = new Email(it);
email.setSubject("Email subject here");
email.setBody("Email body here");
mailServer.send(email);
}

Please try this and let know if issue persists.

Hi @saravp2020 ,

Your comment really helpful, 

could you please advise in case of multi select user-picker:

How to collect all userSelected.getEmailAddress() values into one line? I have 4 people selected in multi select user-picker.

Currently I get result as four different values, like:

email1@company.com

email2@company.com

email3@company.com

email4@company.com

but I'd like them to be together with comma (,) separated, like:

email1@company.com,email2@company.com,email3@company.com,email4@company.com

 

I tried to do as: def emailTo = userSelected.getEmailAddress() + "," + userSelected.getEmailAddress()

But it did not help :( It adds the same value into variable 'emailTo'

 

if(cfValList){
         for(cfVal in cfValList){
                      ApplicationUser userSelected = (ApplicationUser) cfVal;
                      if(userSelected){
                     def emailTo =  userSelected.getEmailAddress() + "," + userSelected.getEmailAddress()

            }
       }
}

Hi @Vladislav


I have just modified the earlier code according to your need, please try below code, and the variable 'recipients' will hold the comma separated email addresses. I believe there could be a bug the way it's concatenated earlier and assigning the value to the same variable in loop.

import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;

CustomField cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_xxxxx");
def issueMgr = ComponentAccessor.getIssueManager();
Issue issue = issueMgr.getIssueObject("<IsseKey>");
def cfValList = issue.getCustomFieldValue(cf);
def recipients = "";

if(cfValList){
for(cfVal in cfValList){
ApplicationUser userSelected = (ApplicationUser) cfVal;
if(userSelected){
if (recipients == ''){
recipients = userSelected.getEmailAddress();
} else {
recipients = recipients + ',' + userSelected.getEmailAddress();
}
}
}
log.warn(recipients);
}
Like # people like this

Hi @saravp2020 ,

Thank you very much for your help!

I really appreciate it!

Like saravp2020 likes this
0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jun 28, 2017

Assuming customfield_12202 is a user-picker, then your code will have a user object in the variable userName, not just a string, and then you dont need  the "def user" line to do any look up.

I'd suggest

Issue issue
UserUtil userUtil = ComponentAccessor.userUtil
def customField = ComponentAccessor.getComponent(CustomFieldManager).getCustomFieldObject("customfield_12202")
def myUser = customField.getValueFromIssue(issue)
log.info "check this user: " + myUser.getUserName() 

Hi Nic,

I tried that but i am still getting the same error.i put the below code in a workflow postfunction just to test whether i am getting the value or not in the logs.

yes 

customfield_12202 is a user picker .
2017-06-28 15:03:06,837 atlassian-scheduler-quartz1.local_Worker-2 ERROR alenka     [scriptrunner.jira.workflow.ScriptWorkflowFunction] Script 
function failed on issue: JUA-12, actionId: 71, file: <inline script>
java.lang.NullPointerException
        at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.getValueFromIssue(AbstractSingleFieldType.java:79)
        at com.atlassian.jira.issue.fields.CustomFieldImpl.getValue(CustomFieldI
mpl.java:454)
        at com.atlassian.jira.issue.fields.CustomFieldImpl.getValueFromIssue(CustomFieldImpl.java:1467)
        at com.atlassian.jira.issue.fields.renderer.RenderableField$getValueFromIssue$0.call(Unknown Source)
        at Script272.run(Script272.groovy:17)

code i used

Issue issue
UserUtil userUtil = ComponentAccessor.userUtil
def customField = ComponentAccessor.getComponent(CustomFieldManager).getCustomFieldObject("customfield_12202")
def myUser = customField.getValueFromIssue(issue)
log.info "check this user: " + myUser

 

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jun 29, 2017

I've just realised something...

You are on JIRA 7.3 or 7.4 aren't you?  My code is for 7.2

no I am trying to get it for 6.4 and also 7.1

 

Abyakta

Try this:

 
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser;


def customFieldManager = ComponentAccessor.getCustomFieldManager()
def devOwner = customFieldManager.getCustomFieldObjectByName("UserPicker CF")

String username = issue.getCustomFieldValue(devOwner).name
ApplicationUser user = ComponentAccessor.getUserManager().getUserByName(username);


log.error "Name: " + user.getDisplayName()
log.error "Username: " + user.name

Hello @Jason Liwag 

Please advise how would you get list of email addresses from "UserPicker CF Multi-select"?

Like odmin likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events