script runner validator- select list field is empty

fjodors
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.
August 31, 2014

Hello

I am trying to create script runner validator that should check select list custom field and another field based on select list field.

E.g. I have select list FieldA with values "Yes", "No" and "None" (empty). I need to check if FieldA is "Yes", then "FieldB" should be > 0. So my current working validator is

(cfValues['FieldA'].getValue() == "Yes" && cfValues['FieldB'] > 0) || (cfValues['FieldA'].getValue() == 'No')

But I need also to check if fieldA is "None", and I am unable to find correct statement for it I tried

(cfValues['FieldA'].getValue() == "Yes" && cfValues['FieldB'] > 0) || (cfValues['FieldA'].getValue() == 'No') || (cfValues['FieldA'] == null)

but it seems it is doesn;t work

What is the correct statement? Is it possible to optimize validator expression (without writing expression in one row) e.g.:

if(fieldA=="Yes"){
   if(fieldB<=0){
      //Validator was NOT passed
      // show message
   }
   else{
      //validator was passed
   }
}
else{
   //validator was passed
}

Thank you in advance,

Fyodor

5 answers

1 accepted

2 votes
Answer accepted
JamieA
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.
August 31, 2014

It's better to use Groovy Truth than comparisons with null. So to check if it has a value:

if (cfValues['FieldA']) ...

to check if it doesn't have a value:

if (! cfValues['FieldA']) ...

You don't need to write it in one line, if you hit enter the text box will expand. Seems to be a common misconception that it must be a one-liner... bad UI design probably.

If it's clearer to write it in 10 lines with "if"s and "return"s then definitely do that.

0 votes
Deleted user March 14, 2018

@JamieA I have a Q too:

 

 I would like to reject the option for setting the "Fixed in version/s" filed as long as the "Affects Version/s" field is empty

how should I write it with script validator? 

fjodors
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.
March 14, 2018

Hi Shelly


If you trying to update field's value based on another field, I think it will be better to use post-function or listener, not validator

If you need validator that checks issue's fields you can use something like:

result=true
if (cfValues['CustomFieldA']){
    if (cfValues['CustomFieldB'].getValue() == "Yes") {
            if(cfValues['CustomFieldA'] <= 0){
                result=false;
            }

    }
}
return result;

 

This °validator checks "CustomFieldA" (number custom field) value when 'CustomFieldB' (select custom field) has YES value.

Deleted user March 14, 2018

Thanks Fyodor,

 

What I need is that:

if field A is empty/null than field B will be grey out - meaning, there will be no option to add value to this field. or make field B disappear 

fjodors
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.
March 14, 2018

 

Hmm....

I think it could be configured by behavior (Manage Addons -> behaviors....), but I haven't tried this functional yet....

Other possible soltuion - create postfunction or listener that updates one field value based on another field value.
Something like this listener example:

.....
MutableIssue issueToUpdate = (MutableIssue) event.issue;
def acceptedIssueTypes = ["typeA", "typeB"];
if(issueToUpdate.getIssueType().name in (acceptedIssueTypes)){

//some value
def someval = 100;

//find specific custom field
def CF = customFieldManager.getCustomFieldObjectByName('CF');

//set CF field and fire update issue event
issueToUpdate.setCustomFieldValue(CF, someval);
issueManager.updateIssue(event.getUser(), issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false);

}
0 votes
fjodors
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.
September 7, 2014

Hello Jamie

Thank you for assistance, it works.

 

Fyodor

0 votes
fjodors
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.
August 31, 2014

Hello Jamie and Simon.

Thank you for fast reply.

Could you describe me how can I put multi-row code? Expression should return TRUE (see screenshot), so I don't understand how can I put code with if, else etc ?

Could you show me an example?

Plugin version is 2.1.17

Best regards,

Fyodor

JamieA
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.
August 31, 2014

Like:

if (1) {
    return true
}
else {
    return false
}

am I missing someting?

The return keyword is actually optional for the last statement executed, but you can put it in for clarity.

0 votes
Simon Kegel //SEIBERT/MEDIA
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.
August 31, 2014

Hey there,

"!= null" instead off ">0" could work and
you could check if you imported all nessesary data.
Like "import com.atlassian.jira.issue.Issue;"

That was often my problem ..

Hope this can help.

Greets
Simon

Suggest an answer

Log in or Sign up to answer