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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to add a value to a user picker field (single)

Edited

Hello,

 

I am trying to get the value out of a text field (it should be an email address) and get the user name out of that email and add it to a single user picker field. Issue creation works well, but the user is not added.

My code is the following:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.PriorityManager
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.bc.user.search.UserSearchService
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser;

def userName = ""
def emailAddress = ""

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def ContactPerson = customFieldManager.getCustomFieldObjectsByName("Contact Person")
def ContactPersonValue = issue.getCustomFieldValue(ContactPerson).toString()
def CreatorCF = customFieldManager.getCustomFieldObject("customfield_16219")
log.warn("Show ContactPersonValue" + ContactPersonValue)

def atStartingPosition = ContactPersonValue.indexOf("@")
def valueWithoutDomain = ContactPersonValue.substring(0, atStartingPosition-1);
def dot = "."

if (valueWithoutDomain.contains(dot)) {

emailAddress = ContactPersonValue
}

else { userName = valueWithoutDomain }

def userSearchService = ComponentAccessor.getComponent(UserSearchService)

if (emailAddress != "") {
def user = userSearchService.findUsersByEmail(emailAddress)
issue.setCustomFieldValue(CreatorCF, (ApplicationUser)user);
}
else {
def ApplicationUser user = ComponentAccessor.userManager.getUserByName(userName)
issue.setCustomFieldValue(CreatorCF, (ApplicationUser)user);
}

 

 

The error that I see after the script is executed is: 

 

2021-03-08 16:46:22,760 WARN [runner.ScriptBindingsManager]: Show ContactPersonValuemihai.arama@externals.hilti.com
2021-03-08 16:46:22,830 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed for user 'ARAMMIH'. 
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[ARAMMIH(JIRAUSER83105)]' with class 'java.util.ArrayList' to class 'com.atlassian.jira.user.ApplicationUser' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: com.atlassian.jira.user.ApplicationUser(com.atlassian.jira.user.DelegatingApplicationUser)
at Script162.run(Script162.groovy:35)

 

Thank you for your suggestions!

1 answer

1 accepted

1 vote
Answer accepted

userSearchService.findUsersByEmail() might return 0, 1 or many users. So it will always be in the form of an array. If you are 100% confident to always get 1 and only one, you might be able to assume you'll be safe just taking the first value in the array.

But I would suggest something along those lines:

def matchingUsers = userSearchService.findUsersByEmail(emailAddress)
if(matchingUsers){
if(matchingUsers.size() == 1){
issue.setCustomFieldValue(CreatorCF, matchingUsers.first() );
} else {
log.warn "More than 1 user with email=$emailAddress were found in Jira. Not sure which is the correct user"
}
} else {
log.warn "No users with email=$emailAddress were found in jira"
}

Thank you very much, that was indeed the cause and your solution is very elegant.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

693 views 7 23
Read article

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