Forums

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

compare custom filed value to a string

Olivera Tancheva January 11, 2019

I have a custom field with multiselect options. I want to as a transition comparing the selected value of the custom field with a given String.

i did the following:

from com.atlassian.jira.component import ComponentAccessor;
from com.atlassian.jira.issue.fields import CustomField;
 
def value = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12032"));    
 

if (value != null && "Business Trips".equals(value[0].getName()) {
    return "This is not a valid vendor!";
}

 

But the transition does not work on Create. Any ideas?

1 answer

0 votes
Ivan Tovbin
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.
January 11, 2019

Hi Olivera,

It appears that you are trying to write a scripted validator, yes? Here's what you need to fix it:

import com.atlassian.jira.component import ComponentAccessor
import com.opensymphony.workflow.InvalidInputException

 
def value = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject(12032.toLong()));    
 

if (value && value[0].getValue().equals("Business Trips")) {
    throw new InvalidInputException("This is not a valid vendor!")
}
return true

Please keep in mind, that since your custom field is a multi-select type, it's value is a collection of options. The code above always checks only the first selected option.

Olivera Tancheva January 11, 2019

It still does not work. Do you have any resources with code so I can learn?

Like Ahsan likes this
Ivan Tovbin
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.
January 11, 2019

Can you please give me more info? What does it say? Does it let you transition your issue? What is the value of your custom field when you are trying to transition your issue?

Cheers.

Olivera Tancheva January 11, 2019

When testing the exception message does not show/ It gives an error:

 

"We can't create this issue for you right now, it could be due to unsupported content you've entered into one or more of the issue fields. If this situation persists, contact your administrator as they'll be able to access more specific information in the log file."

Olivera Tancheva January 11, 2019

Can you specify what type of validator is? Is it a Jython script? A custom one?

Ivan Tovbin
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.
January 11, 2019

To narrow down the list of possible reasons for this behaviour, if you remove the validator for your 'Create' tansition, does it allow you to create issues after that?

Olivera Tancheva January 11, 2019

Yes. It seems that the validator poses the problem. I put the code into a Jython script.

Ivan Tovbin
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.
January 11, 2019

oh, Jython. I thought you had Scriptrunner. Unfortunately i'm not familliar with Jython, but looking at your original code I see at least one mistake. In your 'if' statement change 'getName()' method to 'getValue()' and see if that helps. Or if you have Scriptrunner, use that instead and use my code for it.

Like Olivera Tancheva likes this
Olivera Tancheva January 11, 2019

I have Scriptrunner and when trying to add your code it gives an error at line:

f (value && value[0].getValue().equals("Business Trips"))

value[0] is not allowed.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events