I have a select list custom field named "Change" with only 2 options (Yes, No). The default is set to No. On a transition I want to update the value of Change to Yes using a script post function.
Here's my attempt:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def changeCf = customFieldManager.getCustomFieldObjectByName("Changes")
issue.setCustomFieldValue(changeCf, 10704)
10704 is the id of the option Yes. It shows error when I try to execute the transition. What do I need to change?
You don't need to import the 2nd line.
issue.SetCustomFieldValue() just changes the value, but it doesn't hold it.
To achieve this you could use the IssueManager class.
It would be something like this:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
//either get the current user
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
//or use a admin user to execute this
/*
//get admin User
ApplicationUser adminUser = userManager.getUserByName('admin');
*/
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager();
def changeCf = customFieldManager.getCustomFieldObjectByName("Changes")
issue.setCustomFieldValue(changeCf, "Yes")
issueManager.updateIssue(adminUser, issue, com.atlassian.jira.event.type.EventDispatchOption.ISSUE_UPDATED, false);
Is the last line used to hold the value after the change?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This is the error that I got:It looks like this line causes the error
issue.setCustomFieldValue(changeCf, "Yes")
I think setCustomFieldValue take parameter as object type Option, not string. Any idea on how to fix this?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes the last line is used to hold the value.
There are several ways to do this, examples: Change Holder, IssueManager.UpdateIssue(), or either IssueService Class (which is what JIRA actually uses).
Sorry my mistake that requires an option,as I was using that line by getting the value as:
setCustomFieldValue(field, anotherField.getValue(issue))
You can use this
def yesValue = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {it.toString() == 'Yes'}
and then
issue.setCustomFieldValue(changeCf, yesValue)
issueManager.updateIssue(adminUser, issue, com.atlassian.jira.event.type.EventDispatchOption.ISSUE_UPDATED, false);
Try this, I think it should work
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm getting error on this line
def yesValue = ComponentAccessor.optionsManager.getOptions(changeCf)?.find {it.toString() == 'Yes'}
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 get the config first.
In your script you are trying to find an option directly from the custom field, which isn't valid. You must get the list of options available (aka config) first for that specific issue.
Take a look and let me know how it goes:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yesss that's what I'm missing. Thanks!! It works fine now.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Happy to help
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Please mark my latest reply as the accepted answer, as that was the fixed one :)
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.