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!
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.