set custom field value in post function

Nguyen Tran
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 1, 2018

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?

1 answer

1 accepted

4 votes
Answer accepted
Gezim Shehu [Communardo]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 1, 2018

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);
Nguyen Tran
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 1, 2018

Is the last line used to hold the value after the change?

Nguyen Tran
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 1, 2018

This is the error that I got:Screen Shot 2018-03-01 at 5.02.19 PM.pngIt 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?

Gezim Shehu [Communardo]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 1, 2018

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

Nguyen Tran
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 1, 2018

I'm getting error on this line

def yesValue = ComponentAccessor.optionsManager.getOptions(changeCf)?.find {it.toString() == 'Yes'}

Screen Shot 2018-03-02 at 2.12.43 PM.png

Gezim Shehu [Communardo]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 2, 2018

 

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:

relevantConfig.PNG

Nguyen Tran
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 2, 2018

Yesss that's what I'm missing. Thanks!! It works fine now.

Gezim Shehu [Communardo]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 2, 2018

Happy to help

Gezim Shehu [Communardo]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 2, 2018

Please mark my latest reply as the accepted answer, as that was the fixed one :)

Suggest an answer

Log in or Sign up to answer