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
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.
@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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Jamie
Thank you for assistance, it works.
Fyodor
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Spend the day sharpening your skills in Atlassian Cloud Organization Admin or Jira Administration, then take the exam onsite. Already ready? Take one - or more - of 12 different certification exams while you’re in Anaheim at Team' 25.
Learn more
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.