Where can I get the Options object?

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

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

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

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. 

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

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.

 

 

 

 

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

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. 

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,322 views 14 20
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot