Simple script failed to validate

The following script is calling when the value of  CustomField1 is 'No' also. How to restrict that?
 
cfValues['CustomField1']?.value == 'Yes' && cfValues['Custom Field2']

I want to check if CustomField1 value is yes and CustomField2 is not null

21 answers

1 accepted

actually following should work

cfValues['Downtime Requirement']?.value == 'Yes' && cfValues['Downtime Duration']

can you check space in options of Downtime requirement field or may be spaces added in customfield names, some times this may be root cause

the following code we are using in our java base validator, you can convert/modify to groovy

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.customfields.option.Option;

CustomFieldManager cfm = ComponentAccessor.getCustomFieldManager();
CustomField downtimeRequirement = cfm.getCustomFieldObjectByName("Downtime Requirement");
CustomField downtimeDuration = cfm.getCustomFieldObjectByName("Downtime Duration");
Option drOption = (Option) issue.getCustomFieldValue(downtimeRequirement);
def ddValue = issue.getCustomFieldValue(downtimeDuration);


if(drOption != null && drOption.getValue == 'Yes' && ddValue == null ){
//throw error message
}

Hi Prasad,

Thank you for your comment.

I will test the condition and update. I have one question. If I call a groovy script file instead of a condition, how can i show a validation error message(like the one showed on my screenshot)?

Thanks Prasad,

It is working now :)

if(drOption != null && drOption.getValue() == 'Yes' && ddValue == null ){

invalidInputException = new InvalidInputException (downtimeDuration.getId(),"Please select Downtime Duration")

} else if(drOption != null && drOption.getValue() == 'No' && ddValue != null ){

invalidInputException = new InvalidInputException (downtimeDuration.getId(),"Downtime Requirement should be 'Yes' for selecting Downtime Duration")

}

0 vote

Post the code you're actually using...

Hi Jamie,

The line which I have posted is the only condition. I just wanted to check if the selected radio button option of CustomField1 is 'Yes', then it is manadatory to choose CustomField2 radio. I have used "Script Validator" and added the condition.

0 vote

But you've used a field name of CustomField1, when you said the field name is Downtime Requirement...

Sorry for the confusion. Just to be more clear on AAC, I have referred "Downtime Requirement" as CustomField1.

0 vote

try using asserts in admin->built-in scripts->condition tester. See https://jamieechlin.atlassian.net/wiki/display/GRV/Built-In+Scripts#Built-InScripts-PowerAssertions

I checked with assert but how can we evaluate with null? If no value is selected for "Downtime Duration", the value stored is null but it is not evaluating. Could you please suggest on how to handle null?

assert cfValues['Downtime Requirement']?.value == 'Yes' && cfValues['Downtime Duration'] == null

| | | | |

| No No false false

[Accept ID:null, Affects Build:null, Approved Date:null, Approver :null, Area:[Product Specific], Browser:null, CC List:null, Change Request Category:[null:Production Servers, 1:Software Change], Change Request Plan:null, Compiler:[], Completed By:null, Completed Date:null, Confirmed Date:null, Confirmer:null, Confirmer Build:null, Confirmer Version:null, Database:null, Downtime Duration:null,

Hi Jamie,

Any update please? I am not getting how to handle the null field on this condition. Could you please advise??

Thanks

0 vote

> Any update

Jesus... I do sleep you know.

Can you paste a screenshot of condition tester. I don't believe this eval'd to true.

Please find the screeenshot.

0 vote

It's not showing, can you add it to the question or something, or maybe upload it somewhere else.

Sorry. I just added.. Could you please check the question now?

What I want is the validator should show an error message when the value of customfield1 is 'Yes' and when the value of customfield2 is null but it is showing the error message when the value of customfield1 is 'No' also and when the value of customfield2 is null

0 vote

I'm not sure what you're expecting here - I can see that that evals to false. I can't even remember what the original problem was.

Hi, Jamie,

This is behaving weired. I removed the assert and checked the condition. I am totally confused.. Sometimes with Yes and Sometimes with No I am getting the error message.. :(

I understood from my condition that the whole issue is due to incorrect null checking. Could you please let me know how to check null using script?

Thanks!

Hi Jamie,

The script is !cfValues['Downtime Duration'] && cfValues['Downtime Requirement']?.value == 'No'

Below you can see when i select the radio option Yes, I want to allow the transition to create issue with a downtime duration, but in the screenshot it is showing with error message which is incorrect. Please suggest what is wrong in the above script..

0 vote

Look into ^, which is the xor operator.

You want either Yes and a duration value set, or No and no duration value.

So it should be

Downtme Requirement == No xor Downtime Duration

Hi Jamie,

xor operator also failed. I tested but none of the combination is success though I have set the condition in the same way you suggested.

0 vote

You've just got to think through the logic. Don't try to do it in a one-line...

if (field) {
	check other field
}
else {
	other field should be empty
}

etc

Thank you Jamie,

It is working now with groovy script instead of simple script.

Just posting something related to here so someone could save his/her time.

Scenario: There are two custom date fields, cfA, cfB and one custom radio button group field cfC with options None/Approve/Reject . On a post function transition, I need to copy the value of cfA to cfB if ONLY cfC has its option selected = Approve.

The scriptrunner provides copy field to another with an if condition evaluating to true . Instead of getting the Option first and then checking if its not null and then getting its value (like the code above), this direct call on the  custom radio button group field and checking if it equal to Approve worked fine.

issue.get("customfield_XXXXX") == "Approve" //cfC field

There was no need to use getAt(), getValue() etc..for groovy script here(maybe I got scriptrunner's latest version?)

This worked fine with JiRA 5.2+/Scriptrunner 2.1.15 and JIRA 6+/Scriptrunner 3.0.6

 

 

 

No, it sounds like you are using JIRA Misc Workflow Utilities.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

56 views 0 5
Read article

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