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

This widget could not be displayed.

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

This widget could not be displayed.

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. 

This widget could not be displayed.

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

This widget could not be displayed.

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. 

This widget could not be displayed.

Thank U

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

137 views 2 0
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