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

This widget could not be displayed.

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

}

This widget could not be displayed.

Post the code you're actually using...

This widget could not be displayed.

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.

This widget could not be displayed.

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

This widget could not be displayed.

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

This widget could not be displayed.

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

This widget could not be displayed.

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,

This widget could not be displayed.

Hi Jamie,

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

Thanks

This widget could not be displayed.

> 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.

This widget could not be displayed.

Please find the screeenshot.

This widget could not be displayed.

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

This widget could not be displayed.

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

This widget could not be displayed.

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

This widget could not be displayed.

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.

This widget could not be displayed.

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!

This widget could not be displayed.

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..

This widget could not be displayed.

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

This widget could not be displayed.

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.

This widget could not be displayed.

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

This widget could not be displayed.

Thank you Jamie,

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

This widget could not be displayed.

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
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...

251 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