Where can I get the Options object?

Pierre Gourven January 29, 2016

Hi,

I try to get single list custom field value through Script Runner.
I found some posts which say that this can be done through Options object.
When I try to do this I get this error message:
unable to resolve class Options

Version:
JIRA v7.0.3
JIRA Service Desk v3.0.3

Thanks for your help

Pierre

My code is the following:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.crowd.embedded.api.Group
import com.atlassian.jira.issue.customfields.manager.OptionsManager

MutableIssue issue = issue
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
// Mise à jour du champ TEST_MAJ_SCRIPT_RUNNER champ texte simple
issue.setCustomFieldValue(customFieldManager.getCustomFieldObjectByName("TEST_MAJ_SCRIPT_RUNNER"), "TOTO");
custom_field = customFieldManager.getCustomFieldObjectByName("TECHNOLOGIE"); 
cfConfig = custom_field.getRelevantConfig(issue);
value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find { it.toString() == 'Test'}
issue.setCustomFieldValue(customFieldManager.getCustomFieldObjectByName("TECHNOLOGIE"), value);
Options fieldVal =(Options) issue.getCustomFieldValue(custom_field)

5 answers

0 votes
Pierre Gourven February 1, 2016

Thank U

0 votes
Thanos Batagiannis _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.
February 1, 2016

Pierre,

You get this error because you have not import the Options. But as you can see in the groovy script Kristian provided, you can avoid this kind of compilation errors by using def to declare variables. For your question "how you can write variable value in JIRA Log files " you can just

log.debug("Issue Key is ${issue.key}")

 and enable script runner debugging (Profiling and Logging -> configure new package with package name 'com.onresolve' and set logging lvl to DEBUG). In order to see you log messages you can use the built in script - view server log files. 

0 votes
Pierre Gourven January 29, 2016

It does not work. Is there a way I can write my variable value in JIRA Log files?

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 29, 2016

Script variables are stored in memory when the script is run for the lifetime of the script. You cannot write out to the log file. If you know the value you are searching for then you create a seperate variable with this value in.

 

 

 

 

Pierre Gourven February 1, 2016

I finally did something like

custom_field = customFieldManager.getCustomFieldObjectByName("TECHNOLOGIE");                fieldval = issue.getCustomFieldValue(custom_field);
and I get my result.

Thanks a lot for your help Kristian

0 votes
Pierre Gourven January 29, 2016

Thanks for your help Krisitian.

I have no problem to set the value of my field, which I made with a code like yours.
Now, I want to be able to get the value to use it later in my code.

Pierre

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 29, 2016

In my example code below the value variable has the option value stored and can be used again later in on in the script. If you get the option values how I have below the they will be stored in variables that can be used later. 

0 votes
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 29, 2016

Hi Pierre,

I have enclosed some code below which shows how to set an option for a Multi Select field that was wrote on JIRA 6.4.12 and Script Runner 4.1.3.

The code gets the Client select list field and finds the XX option in it before applying it to the issue. The post function for this code should always be place first in the list of post functions to ensure the field is set before the issue is updated and indexed.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

// Get a pointer to the issue
MutableIssue issue = issue

// Get the Custom Field Manager
def customFieldManager = ComponentAccessor.getCustomFieldManager();

//Get the custcom Field
def custom_field = customFieldManager.getCustomFieldObjectByName("Client");

// Get the Config for the field
def fieldConfig = custom_field.getRelevantConfig(issue)

// Find the option to set
def value = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'XX'}

// Set the value of the field
issue.setCustomFieldValue(custom_field, value)

 

I hope this helps.

Kristian

Suggest an answer

Log in or Sign up to answer