Script post function to update a custom field

Aaron Pascoe March 18, 2015

Hey Atlassian community/Jamie Echlin,

The last couple days I've been trying to update a custom field based off a condition when an issue is created.   This seems like it should be pretty straight forward programmatically using the script runner - custom groovy script post function.  Unfortunately I seem to be doing something wrong.  

 

Here's the exact problem - if the reporter is a member of theGroup -> then add that group to the custom field "additional groups" (a group picker with multiple groups).

 

So for starters I just want to set the additional groups field and I'll worry about getting the conditional statement working afterwards.

This is what I have:

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue

ComponentManager componentManager = ComponentManager.getInstance()
def customFieldManager = componentManager.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjectByName("Additional Groups")
issue.setCustomFieldValue(customField, "theGroup")

 

I'm not getting any errors or really anything to go off of why this isn't working.  Any help would be much appreciated.  

 

Using JIRA 6.3.13 and script runner version 3.0.7

4 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

4 votes
Alejo Villarrubia [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 19, 2015

Hi @Aaron Pascoe,

The problem is in the following line of your script:

issueToUpdate.setCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Additional Groups"), "theGroup");

That custom field you are trying to update is expecting a collection of groups but is only getting a string.

Try replacing that line with the following one and see if you can update the issue:

issueToUpdate.setCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Additional Groups"), ["theGroup"]);
4 votes
dimapasko March 19, 2015

Hi

issue.setCustomFieldValue - won't save you customfield in DB.

You need to save issue explicitly by using MutableIssue, like:

MutableIssue issueToUpdate = (MutableIssue) issue;
issueToUpdate.setCustomFieldValue(customFieldManager.getCustomFieldObjectByName("CUSTOM_FIELD_NAME"), location);
issueManager.updateIssue(user, issueToUpdate, EventDispatchOption.ISSUE_ASSIGNED, false)
Aaron Pascoe March 19, 2015

I thought this was only needed if you were not putting the script in a post function.  As one of the steps in the post function is re indexing the issue to the db

Aaron Pascoe March 19, 2015

Also does 'location' need to be initialized? issueToUpdate.setCustomFieldValue(customFieldManager.getCustomFieldObjectByName("CUSTOM_FIELD_NAME"), *location*);

Aaron Pascoe March 19, 2015

Also what is "user"

0 votes
Ewald_Ninaus October 18, 2015

Hi,

did you find a solution? I have the same Problem: HowTo: change Group Picker (multiple groups) with scriptrunner / post-function

 

0 votes
Aaron Pascoe March 19, 2015

Ok update:

I have been running this in the script console to get a little bit of error reporting:

 
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.type.*
import com.atlassian.crowd.embedded.api.User;
 
ComponentManager componentManager = ComponentManager.getInstance();
IssueManager issueManager = componentManager.getIssueManager();
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();
User user = componentManager.getJiraAuthenticationContext().getLoggedInUser()
MutableIssue issueToUpdate = (MutableIssue) issueManager.getIssueObject("XXXX");
issueToUpdate.setCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Additional Groups"), "theGroup");
issueManager.updateIssue(user, issueToUpdate, EventDispatchOption.ISSUE_ASSIGNED, false);

 

I'm getting this error:

java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Collection

at com.atlassian.jira.issue.customfields.impl.AbstractMultiCFType.createValue(AbstractMultiCFType.java:39)

at com.atlassian.jira.issue.fields.CustomFieldImpl.createValue(CustomFieldImpl.java:831)

at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:505)

at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:487)

at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:879) at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:841)

at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:825)

at com.atlassian.jira.issue.IssueManager$updateIssue$1.call(Unknown Source)

at Script176.run(Script176.groovy:22)

Aaron Pascoe March 20, 2015

And just for reference XXXX is an actual ticket I am referencing. i.e. OPS-1234

Marlene Cote July 13, 2016

Did you every get this to run?  I am trying to set a custom field too.

thanks.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events