Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,551,701
Community Members
 
Community Events
184
Community Groups

Scriptrunner Validator: Check that two checkboxes are not checked

Edited
Kmmaughs
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.
Aug 03, 2020

I have two checkbox fields. If one is checked, the other cannot/should not be checked and vice versa. I have a Behaviour set up to disable one checkbox if the other is checked, but somehow an issue slipped by. To prevent this from happening again, in addition to the Behaviour, I wanted to add a validator to ensure that the two cannot both be checked in order to Create the issue. Therefore, I set up a Custom Validator on the Create transition. It looks like this:

 

import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category;
import com.opensymphony.workflow.InvalidInputException;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.comments.CommentManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.util.ImportUtils;
import com.opensymphony.util.TextUtils
import com.atlassian.jira.component.ComponentAccessor;


def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cfValue1 = customFieldManager.getCustomFieldObjectsByName("Custom Field 1")
def cfValue2 = customFieldManager.getCustomFieldObjectsByName("Custom Field 2")

if(cfValue1== 'Yes'){
if(cfValue2 == 'Yes'){
return false
}
return true
}
else{return true}

 

There's no error being thrown, but the issue can be created with both checkboxes checked (Yes). I would appreciate a suggestion.

 

1 answer

0 votes
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Aug 03, 2020 • edited

Your problem is that value of checkbox custom field is not of String type. I think this custom field contains collection of options. If both fields have only Yes value,the simplest condition is

if(cfValue1 && !cfValue1.isEmpty() && cfValue2 && !cfValue2.isEmpty()){ return false; }

Kmmaughs
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.
Aug 04, 2020

I tried it and tested it by checking both boxes, and the issue was still created. :(

Here's what the validator code looked like:

import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category;
import com.opensymphony.workflow.InvalidInputException;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.comments.CommentManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.util.ImportUtils;
import com.opensymphony.util.TextUtils
import com.atlassian.jira.component.ComponentAccessor;


def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cfValue1 = customFieldManager.getCustomFieldObjectsByName("Custom Field 1")
def cfValue2 = customFieldManager.getCustomFieldObjectsByName("Custom Field 2")

if(cfValue1 && !cfValue1.isEmpty() && cfValue2 && !cfValue2.isEmpty()){ return false;
}

Did I possibly mess it up?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events