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
Community moderators have prevented the ability to post new answers.
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"]);
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Also does 'location' need to be initialized? issueToUpdate.setCustomFieldValue(customFieldManager.getCustomFieldObjectByName("CUSTOM_FIELD_NAME"), *location*);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
did you find a solution? I have the same Problem: HowTo: change Group Picker (multiple groups) with scriptrunner / post-function
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
And just for reference XXXX is an actual ticket I am referencing. i.e. OPS-1234
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Did you every get this to run? I am trying to set a custom field too.
thanks.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Community moderators have prevented the ability to post new answers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.