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

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.

2 answers

2 accepted

You need to update the issue the issue after setCustomFieldValue

IssueManager issueManager=ComponentAccessor.getIssueManager();
issueManager.updateIssue(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(),issueKey,EventDispatchOption.ISSUE_UPDATED,true);

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?

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.

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.

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,760 views 11 18
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot