Forums

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

Why condition doesn't work in my validator?

Alexander
Contributor
February 11, 2019

Hi community!
I have field "Field_1" and "Field_2". I want to during the selection Field_1 and value "BR" could choose only K3 value in Field_2. If the values are selected incorrectly then display an error.
I set script validator in create issue.

My code:

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

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def field_1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("cf1")
def field_2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("cf2")



def field_1_value = issue.getCustomFieldValue(field_1)
def field_2_value = issue.getCustomFieldValue(field_2)



if (field_1_value == "BR" && field_2_value != "К3") {
throw new InvalidInputException("You must pick only K3 value")
}

This script work but the log is empty and condition not run.
I need to get an error in case of a wrong choice.

What i do wrong?

Best Regards,
Alexander


2 answers

1 accepted

0 votes
Answer accepted
Hector
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 12, 2019

Hi @Alexander

May I ask if your custom field/s are Select type ones? If so, the value you get is not a String, but a LazyLoadedOption, so when you compare them with == result is false.

Instead, try to get the Option value using method getValue(), ie:

def field_1_value = issue.getCustomFieldValue(field_1).getValue()

Btw, I would suggest to use assert to confirm and get details about the operation you are executing.

Hope it helps!

Alexander
Contributor
February 12, 2019

Hi @Hector,

Oh, right! Thanks!)

Regards,

Alexander

Alexander
Contributor
February 12, 2019

I did so 

def field_1_value = (String) issue.getCustomFieldValue(field_1)
0 votes
Ravi Varma
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 11, 2019

Hi @Alexander,

Did you debug by checking out the two custom field values selected from the UI, in your script? You need to make sure that the if condition is triggered which is not happening in your case. 

Please make sure that the custom field names are unique and also, typecast as a string such that variable holds the custom field value for comparison. Your debugging should include checking the value for all cases, to make sure that you are capturing the custom field values in the variables defined in your script.

 

Thanks,

Ravi Varma

Suggest an answer

Log in or Sign up to answer