Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy script to assign project lead username to custom field does not work

Deleted user October 18, 2017

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.

4 answers

2 accepted

4 votes
Answer accepted
Deleted user October 19, 2017

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.

Alok Singh October 19, 2017

Cool

0 votes
Answer accepted
Alok Singh October 18, 2017

You need to update the issue the issue after setCustomFieldValue

IssueManager issueManager=ComponentAccessor.getIssueManager();
issueManager.updateIssue(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(),issueKey,EventDispatchOption.ISSUE_UPDATED,true);
Deleted user October 19, 2017

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:

Message:
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?

Deleted user October 19, 2017

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:

Message:
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.

Loganayaki January 25, 2023

Hi,

Any solution for this Error? Pls suggest.

 

java.lang.ClassCastException: com.atlassian.jira.user.DelegatingApplicationUser cannot be cast to java.util.Collection

 

Thanks 

0 votes
Loganayaki January 25, 2023

HI ,

Any solution for this Error. Pls suggest.

java.lang.ClassCastException: com.atlassian.jira.user.DelegatingApplicationUser cannot be cast to java.util.Collection
0 votes
Tomas Arguinzones Yahoo
Contributor
March 2, 2018

Thank you very much for posting Ross! works like a charm

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events