Simple script failed to validate

srinivasp
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 26, 2013
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

0 votes
Answer accepted
RambanamP
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 27, 2013

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
}

srinivasp
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 28, 2013

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

srinivasp
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 30, 2013

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 votes
Sri Kanth March 26, 2015

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

 

 

 

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.
March 26, 2015

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

0 votes
srinivasp
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 30, 2013

Thank you Jamie,

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

0 votes
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.
September 30, 2013

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

0 votes
srinivasp
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 30, 2013

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 votes
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.
September 28, 2013

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

0 votes
srinivasp
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 27, 2013

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 votes
srinivasp
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 26, 2013

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!

0 votes
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.
September 26, 2013

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.

0 votes
srinivasp
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 26, 2013

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 votes
srinivasp
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 26, 2013

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

0 votes
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.
September 26, 2013

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

0 votes
srinivasp
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 26, 2013

Please find the screeenshot.

0 votes
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.
September 26, 2013

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

0 votes
srinivasp
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 26, 2013

Hi Jamie,

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

Thanks

0 votes
srinivasp
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 26, 2013

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,

0 votes
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.
September 26, 2013

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

0 votes
srinivasp
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 26, 2013

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

0 votes
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.
September 26, 2013

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

0 votes
srinivasp
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 26, 2013

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 votes
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.
September 26, 2013

Post the code you're actually using...

Suggest an answer

Log in or Sign up to answer