Hello,
I have a Groovy script that assigns the project lead for an issue to the "Approvers" multi-user picker custom field in JIRA service desk. This script runs in the "Create issue" transition post functions.
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.component.ComponentAccessor
projectLead = issue.getProjectObject().getProjectLead().getKey()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField customFieldApprovers = customFieldManager.getCustomFieldObjectByName( "Approvers" );
issue.setCustomFieldValue(customFieldApprovers, projectLead)
log.debug(issue.getCustomFieldValue(customFieldApprovers).toString())
The debug output of this script successfully outputs the project lead as if it had been assigned to the "Approvers" custom field, but when I check the custom field after creating an issue, it is blank. If I comment out the issue.setCustomFieldValue method, it outputs "null" if there is no value in the "Approvers" custom field and the username as expected, when testing this on issues that I have created and manually assigned a value to the "Approvers" custom field.
What can I be doing wrong here? I've tried moving the scriptrunner to different positions in the post functions list but that doesn't seem to have any effect, and based on the debug output it looks like the script is working, except it's not.
I resolved the problem as follows:
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption
projectLead = issue.getProjectObject().getProjectLead()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField customFieldApprovers = customFieldManager.getCustomFieldObjectByName( "Approvers" );
issue.setCustomFieldValue(customFieldApprovers, [projectLead])
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueManager issueManager=ComponentAccessor.getIssueManager();
issueManager.updateIssue(user,issue,EventDispatchOption.ISSUE_UPDATED,true);
After I added in the updateIssue(...) method, I forgot that I was trying to set the value of a multi-user picker custom field, and not a single-user picker custom field. I needed to add [] brackets around the user I wanted to set the value of the custom field to.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You need to update the issue the issue after setCustomFieldValue
IssueManager issueManager=ComponentAccessor.getIssueManager();
issueManager.updateIssue(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(),issueKey,EventDispatchOption.ISSUE_UPDATED,true);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I did as you suggested:
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption
projectLead = issue.getProjectObject().getProjectLead().getKey()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField customFieldApprovers = customFieldManager.getCustomFieldObjectByName( "Approvers" );
issue.setCustomFieldValue(customFieldApprovers, projectLead)
IssueManager issueManager=ComponentAccessor.getIssueManager();
issueManager.updateIssue(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(),issue,EventDispatchOption.ISSUE_UPDATED,true);
//log.debug(issue.getCustomFieldValue(customFieldApprovers).toString())
But now I get an error:
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Collection
I don't see where I'm typecasting a String to a Collection?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It's definitely coming from the updateIssue method - when I switch to the following:
issueManager.updateIssue(projectLead,issue,EventDispatchOption.ISSUE_UPDATED,true);
I now get:
java.lang.ClassCastException: com.atlassian.jira.user.DelegatingApplicationUser cannot be cast to java.util.Collection
So it says updateIssue is expecting a collection, but in the documentation I see it asks for a variable of type ApplicationUser. I'm confused.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Any solution for this Error? Pls suggest.
java.lang.ClassCastException: com.atlassian.jira.user.DelegatingApplicationUser cannot be cast to java.util.Collection
Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
HI ,
Any solution for this Error. Pls suggest.
java.lang.ClassCastException: com.atlassian.jira.user.DelegatingApplicationUser cannot be cast to java.util.Collection
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you very much for posting Ross! works like a charm
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.