Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Scriptrunner Validator: Check that two checkboxes are not checked Edited

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

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; }

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
Community showcase

The benefits of using Jira in different departments

Jira is a great tool to use across different departments. Forget that paperwork – switch to Jira and get that tasks done smoothly. Marketing Jira allows for a complete digital transformation of you...

119 views 0 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you