Hello Everybody!
I am trying to create a post function script that takes a user name from a custom field (single text line), searches for that user in our user base and then sets the respective user as a Project Lead in a User Picker field.
What I have so far is this:
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
//import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager;
import org.apache.log4j.Logger
import org.apache.log4j.Level
def log = Logger.getLogger("com.acme.testing")
log.setLevel(Level.DEBUG)
def projectManager = ComponentAccessor.getProjectManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def userManager = ComponentAccessor.getUserManager()
//def issue = thisissue as MutableIssue
//define the fields
def projectLead = 14610 as Long
def mlProjectLead = 17100 as Long
//ApplicationUser appUserLogged = userManager.getUserByName("gabriel.udvar")
//log.warn 'log user '+appUserLogged.getClass()
//def issue = issue as MutableIssue
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey('TEST_SS_ENH-1')
def mlProjectLeadText = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(mlProjectLead)).toString()
def projectLeadField = customFieldManager.getCustomFieldObject(mlProjectLead)
def userToSet
try {
def projectLeadUser = ComponentAccessor.getUserManager().getUserByName(mlProjectLeadText)
def mlUser = ComponentAccessor.getUserManager().getUserByName('default.user') //custom field String value set by an external system through Rest API
if (ComponentAccessor.getUserManager().isUserExisting(projectLeadUser)){
userToSet = projectLeadUser
} else {
userToSet = mlUser
}
issue.setCustomFieldValue(projectLeadField, userToSet)
issueManager.updateIssue(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), issue, EventDispatchOption.ISSUE_UPDATED, false);
} catch (IllegalStateException e) {
e.printStackTrace();
}
The error that I get is that the updateIssue method needs an ApplicationUser object and it seems that both userManager.getUserByName() and getJiraAuthenticationContext().getLoggedInUser() are returning DelegatingApplicationUser objects which are not as expected.
The API documentation says that both methods should return ApplicationUser objects.
Below is the complete error that I get in the logs:
2021-03-22 18:49:57,395 ERROR [common.UserScriptEndpoint]: Script console script failed: java.lang.ClassCastException: com.atlassian.jira.user.DelegatingApplicationUser cannot be cast to java.lang.String at com.atlassian.jira.issue.customfields.impl.RenderableTextCFType.valuesEqual(RenderableTextCFType.java:20)
at com.atlassian.jira.issue.fields.ImmutableCustomField.valuesEqual(ImmutableCustomField.java:1575)
at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:424)
at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)
at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:728)
at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:681)
at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:667)
at com.atlassian.jira.issue.managers.RequestCachingIssueManager.updateIssue(RequestCachingIssueManager.java:217)
at com.atlassian.jira.issue.IssueManager$updateIssue$1.call(Unknown Source) at Script345.run(Script345.groovy:42)
Is there something that I'm missing? Is there any way I can cast the DelegatingApplicationUser to an ApplicationUser?
I'm using Jira 8.12.3.
Thanks a million!
Gabriel
Found the issue the same day with the help of a colleague.
I was trying to write the ApplicationUser object in the wrong custom field that was accepting text, thus rendering the "cannot be cast to java.lang.String" error.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.