Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to get the status of an option in a scheme of a custom field

Lutz_Pliske April 21, 2020

Hi all,

I am a noob at this and hope for your help:

I am working on a verification script for an app (worklog pro) but I believe this is generic JIRA logic that I just don't get.

My situation: I have a custom field that has several schemes, with options and some will get disabled. I want to get this status information and react on it.

What I have received with my tests so far was only the first easy start:

The project, the issue, the custom field plus its value:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;


def issue = worklog.getIssue();
def project = issue.getProjectObject();


def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cfReference = customFieldManager.getCustomFieldObject("customfield_11300")

def reference = issue.getCustomFieldValue(cfReference)

return project.getKey() + ", Reference: " + reference;

What I want to know is: Is the option reference enabled or not?

Steps I need to take for this:
- Give me the scheme for the custom field "cfReference" that is valid in the project "project".

- Give me the status of the option "reference" in this scheme

 

Hope someone can help on this :-)

Thanks in advance!

 

2 answers

1 accepted

0 votes
Answer accepted
Lutz_Pliske May 6, 2020

Solution

def referenceFieldObject = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("NameOfCustomField");
def referenceFieldValue = (Option) issue.getCustomFieldValue(referenceFieldObject);
if ((referenceFieldValue !=null) && (referenceFieldValue.getDisabled()==true)){
return "No logging possible: Value has been disabled / is not valid any more.";}

0 votes
Deniz Oğuz - The Starware
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.
April 21, 2020

 

You can use following code as a starting point.

FieldLayoutManager fieldLayoutManager = ComponentAccessor.getFieldLayoutManager();
OptionsManager optionsManager = ComponentAccessor.getOptionsManager();
CustomField singleSelect = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11300");


FieldConfigSchemeManager fieldConfigSchemeManager = ComponentAccessor.getFieldConfigSchemeManager();
List<FieldConfigScheme> configSchemesForField = fieldConfigSchemeManager.getConfigSchemesForField(singleSelect);
FieldConfigScheme fieldConfigScheme = configSchemesForField.get(0);
Options options = optionsManager.getOptions(fieldConfigScheme.getOneAndOnlyConfig());
options.get(2).getDisabled();

 

Lutz_Pliske April 21, 2020

Hi Deniz,

this one time I tried not to get on your nerves and still you help me :-) ... Thank you so much for such a quick response. I am still having trouble since I just don't understand the object model and also don't really understand where to look for more information. What I found about the class / function description is just too nerdy for me. Anyhow, I have come this far - not understanding some part in the middle - but hoping to get there anyhow with your help ... lol. It is giving me the error:

No signature of method: java.lang.Boolean.contains() is applicable for argument types: (com.atlassian.jira.issue.customfields.option.LazyLoadedOption) values: [STAR intern - no billing] Possible solutions: toString(), toString(), toString(), notify(), toString(boolean)

... I also tried to use toString() but it did not help.

Code:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;


def issue = worklog.getIssue();
def project = issue.getProjectObject();


def customFieldManager = ComponentAccessor.getCustomFieldManager();
def fieldLayoutManager = ComponentAccessor.getFieldLayoutManager();
def optionsManager = ComponentAccessor.getOptionsManager();
def fieldConfigSchemeManager = ComponentAccessor.getFieldConfigSchemeManager();

def cfReference = customFieldManager.getCustomFieldObject("customfield_11300")
def configSchemesForField = fieldConfigSchemeManager.getConfigSchemesForField(cfReference);

def fieldConfigScheme = configSchemesForField.get(0);
def options = optionsManager.getOptions(fieldConfigScheme.getOneAndOnlyConfig());
def allDisabled = options.get(2).getDisabled();

def reference = issue.getCustomFieldValue(cfReference)

if (allDisabled.contains(reference)) {
return "Failure: This Option is disabled";
}

Thanks!

Deniz Oğuz - The Starware
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.
April 21, 2020

I'm glad if I could help :) But I'm not sure whether I understand your request correctly or not. Jira UI don't allow you to select a disabled option, so why do you need to check whether a disabled option is is selected. 

Following code will determine selected value of a  "single select field" is disabled option or not. 

You may need to adjust field config related part if you have more than one field configuration for a field. That is different options for different projects. I have added a reference to related Javadoc just above it. In the below code I simply get first field configuration (ssConfigSchema.get(0)).

OptionsManager optionsManager = ComponentAccessor.getOptionsManager();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
FieldConfigSchemeManager fieldConfigSchemeManager = ComponentAccessor.getFieldConfigSchemeManager();

CustomField singleSelectField = customFieldManager.getCustomFieldObject("customfield_11300");
Object selectedValue = issue.getCustomFieldValue(singleSelectField);
if (selectedValue != null) {
List<FieldConfigScheme> ssConfigSchema = fieldConfigSchemeManager.getConfigSchemesForField(singleSelectField);

//https://docs.atlassian.com/software/jira/docs/api/8.0.0/com/atlassian/jira/issue/fields/config/manager/FieldConfigSchemeManager.html

FieldConfigScheme fieldConfigScheme = ssConfigSchema.get(0);
Options options = optionsManager.getOptions(fieldConfigScheme.getOneAndOnlyConfig());
List<Option> disabledOptions = options.stream().filter(option -> option.getDisabled()).collect(Collectors.toList());
boolean selectedDisabledOption = disabledOptions.stream().anyMatch(option -> option.equals(selectedValue));

if
(selectedDisabledOption) {
//return "How can you able to select a disabled option?";
}
Like Lutz_Pliske likes this
Deniz Oğuz - The Starware
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.
April 21, 2020

Hi Lutz,

I'm glad if I could help. Following code find whether selected option is disabled or not. Jira UI doesn't allow selection of disabled options, so why do you need this?

Also below code assumes there is only one field config schema for custom field this is why it uses ssConfigSchema.get(0). If you have more than one you need to find correct one, https://docs.atlassian.com/software/jira/docs/api/8.0.0/com/atlassian/jira/issue/fields/config/manager/FieldConfigSchemeManager.html may help.

 

OptionsManager optionsManager = ComponentAccessor.getOptionsManager();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
FieldConfigSchemeManager fieldConfigSchemeManager = ComponentAccessor.getFieldConfigSchemeManager();

CustomField singleSelectField = customFieldManager.getCustomFieldObject("customfield_11300");
Object selectedValue = issue.getCustomFieldValue(singleSelectField);
if (selectedValue != null) {
List<FieldConfigScheme> ssConfigSchema = fieldConfigSchemeManager.getConfigSchemesForField(singleSelectField);

FieldConfigScheme fieldConfigScheme = ssConfigSchema.get(0);
Options options = optionsManager.getOptions(fieldConfigScheme.getOneAndOnlyConfig());
List<Option> disabledOptions = options.stream().filter(option -> option.getDisabled()).collect(Collectors.toList());
boolean selectedDisabledOption = disabledOptions.stream().anyMatch(option -> option.equals(selectedValue));
if (selectedDisabledOption) {
//return "How can you able to select a disabled option?";
}
Like Lutz_Pliske likes this
Lutz_Pliske April 23, 2020

Thanks - I will now take a look at that. Since you are interested in our usecase:

We have vaules that get disabled after a while when the contract is finished. Goot: Now no new issue can be created using this value BUT existing issues still have this value and people can log time on this issue. :-(

Deniz Oğuz - The Starware
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.
April 23, 2020

Just as a reminder, you can also use specific workflow status for these issues and prohibit worklog entry or editing of the issue completely using workflow properties.

Like Lutz_Pliske likes this
Lutz_Pliske April 23, 2020

But I still would have to find a way how to automatically set the status / transition for all those issues when the option of my custom field scheme gets disabled, right? I don't see how I could get there - happy if there is an easier way then the one above ;-)

Btw: Yes, I have several schemes in the custom field definition.

Lutz_Pliske May 6, 2020

A developer helped me to fix it - it was pretty simple :-)

def referenceFieldObject = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("NameOfCustomField");
def referenceFieldValue = (Option) issue.getCustomFieldValue(referenceFieldObject);
if ((referenceFieldValue !=null) && (referenceFieldValue.getDisabled()==true)){
return "No logging possible: Value has been disabled / is not valid any more.";}

Suggest an answer

Log in or Sign up to answer