script runner validator- select list field is empty

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

This widget could not be displayed.

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.

This widget could not be displayed.

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

This widget could not be displayed.

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

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.

This widget could not be displayed.

Hello Jamie

Thank you for assistance, it works.

 

Fyodor

This widget could not be displayed.

@Jamie Echlin [Adaptavist] 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? 

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.

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 

 

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);

}

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Tuesday in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

206 views 1 3
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