Validators Checkboxes JMWE

Tyas Iglecias February 2, 2020

I want create validation  with condition :

Custom field - Checkboxes : Deployment 

Options :

- DR

- DC

- None

the question is how to make pop up error if user choose DR&None / DC&None/ DR&DC&None 

I try used 

(issue.customfield_12308.some (it => it.value == "None"))

Its work  in Test Jira Expression, but its not work after publish the workflow

3 answers

1 accepted

0 votes
Answer accepted
Tyas Iglecias February 2, 2020

Hi David,

what I want is pop up error if user choose

DR&None

DC&None

DR&DC&None 

Empty

 

and the status can be contiune if user choice

DR

DC

DC&DR

None

 

What the meaning "!!" / "!" ? 

!issue.customfield_12308.some(it => it.value == "None"
!!issue.customfield_12308 
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 3, 2020

! means "not", and thus "!!" means "not not" which really means "is true". "!!" is needed to transform any "truthy" value into a real "true" value (because Jira expressions support "truthy" and "falsy" values during the evaluation of the expression, but Conditions and Validators only support "true" or "false" as the returned value)

If you need to enforce selecting at least one value, then try this:

!!issue.customfield_12308 && issue.customfield_12308.length > 0 &&
(!issue.customfield_12308.some(it => it.value == "None") || issue.customfield_12308.length == 1)
Like Tyas Iglecias likes this
Tyas Iglecias February 3, 2020

Its Work thanks David :D

Darryl Lee July 1, 2020

Glad I finally found this answer. It would be great if this was in the KB for JMWE Cloud.

Also, do you know why the include function doesn't work like I would expect it to? The syntax of the some function always confuses me.

Thanks!

Radhika Vijji _Innovalog_
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.
July 1, 2020

Hi Darry,

Because it is Jira expressions (used in Conditions and Validators of Cloud) and not Nunjucks (used in post-functions). See here for information on Jira expressions.

Regards,

Radhika

Darryl Lee July 2, 2020

Weird, because that link says:

List literals
Lists can not only be obtained from context objects but also created manually. For example, to check if the issue type is either a Bug or Task, create a list with these two types and use the includes() method to test if the actual value is one of the two listed:

['Bug', 'Task'].includes(issue.issueType.name)

And on another page:

 

  • some(Any => Boolean): Checks if the list contains at least one element that satisfies the given predicate (Boolean).
  • includes(Any): Checks if the given argument is stored in the list (Boolean).

I think maybe I'm not understanding what the argument for includes() should look like. From the first example, I guess something like this might work?

['None'].includes(issue.customfield_12308.value)

Guess I'll play around with it tomorrow.

Radhika Vijji _Innovalog_
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.
July 2, 2020

Hi Darry,

My bad. My statement was incorrect. As mentioned in the document, using include you could check if the actual value is one of the values listed. Whereas some is a closure that checks if the list contains at least one element that satisfies the given predicate. So in the above example, in which you want to check that "None" option is selected, some should be used to check for the value of the option, which you cannot do using include

Regards,

Radhika

Darryl Lee - CCC July 7, 2020

I guess I still don't understand the difference between includes and some.

It seems like includes should work the same way some is currently working. As the documentation says: 

  • includes(Any): Checks if the given argument is stored in the list (Boolean).

I tried the different syntax ['None'].includes(issue.customfield_12308.value), but didn't have any success.

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 7, 2020

What is the custom field type of customfield_12308? Is it a single-select field or a multi-valued field (e.g. Checkboxes)?

Also, is "None" (in your case) an actual "Option" of the custom field, or do you mean it as "no value selected"?

Darryl Lee - CCC July 7, 2020

Hi David - it's a multi-valued Checkbox field. "None" is an actual option.

Like Tyas Iglecias likes this
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 7, 2020

Then issue.customfield_12308.value is invalid, since issue.customfield_12308 returns a List of values, as shown on the Issue Fields help tab below the editor. Please use that help tab to insert the correct Jira expression for your custom field. 

Darryl Lee - CCC July 7, 2020

If I wanted to use includes, what would the correct syntax be?

I think I tried this, initially:

issue.customfield_12308.includes('None')

And it worked for the positive case (when None was checked), but I was trying to find the dispositive (?) (None not checked) and was having problems with I think this:

!issue.customfield_12308.includes('None')
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 8, 2020

You cannot use include with multi-valued fields. The correct syntax to check multi-valued fields such as Checkboxes is shown on the Issue Fields help tab:

image.png

In your case, you'd want:

!!issue.customfield_12308 && issue.customfield_12308.some(it => it.value == "None")

And the opposite test would be:

!issue.customfield_12308 || !issue.customfield_12308.some(it => it.value == "None") 
Darryl Lee - CCC July 8, 2020

Sorry, I'm being dense, but I think I'm getting it (slowly).

So just for clarification, is the reason includes won't work because multi-value fields are not actually List literals (arrays), but rather something like a Dict (key-value pairs), hence the requirement to do the iterations with some?

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 8, 2020

Not exactly. It's because multi-valued fields (multi-select and checkboxes, version pickers, user pickers, etc.) return a List of objects, not string literals. For example, checkboxes return a List of "Option" objects (in the form of a JavaScript hash, or JSON object, however you want to look at it) that contain 3 properties: id, value and "self" (a URL):

[
  {
    "self": "https://jmwe-dev-local.atlassian.net/rest/api/2/customFieldOption/10100",
    "value": "Radio1",
    "id": "10100"
  },
  {
    "self": "https://jmwe-dev-local.atlassian.net/rest/api/2/customFieldOption/10101",
    "value": "Radio2",
    "id": "10101"
  }
]

 See https://developer.atlassian.com/cloud/jira/platform/jira-expressions-type-reference/#issue for details (under "custom fields")

Like Darryl Lee - CCC likes this
Darryl Lee - CCC July 8, 2020

Thanks, this is really helpful! Heh, the documentation, not so much:

In addition to these, all custom fields are available and can be referenced by one of the following:

  • ID: issue.customfield_10010
  • key: issue['com.my.app.field-key']

Custom fields are returned as JSON values, using exactly the same format as in the issue REST API.

I mean I guess if I was intimately familiar with the REST API I would know they were returned as objects.

Anyways, I really do appreciate your taking the time to explain!

0 votes
Ernesto Donate November 18, 2020

Hello @David Fischer @Radhika Vijji _Innovalog_ 

Wondering if  you can please help me?

I'm trying to build a validation that will block a transition for a specific Issue type when it meets certain conditions. 

IF

  • Issue Type Name = "New Award"
  • Sponsor Code(single text field) has one of the following values (5101,5105,5106, etc...)

Then 

Block Transition when

  • Multi Valued Check Box field "Check all that apply" does not have the value "X" checked. In other words, the person needs to at least select the value "X", otherwise, the transition is blocked.

What I've done so far is tried to use the Field Required Validator with the following condition:

(issue.issueType.name=="New Award"
&& (!issue.customfield_10117 || !issue.customfield_10117.some(it => it.value == "X"))
&& (issue.customfield_10075 == "5101"
||issue.customfield_10075=="5105"
||issue.customfield_10075=="5106"
||issue.customfield_10075=="5107"
||issue.customfield_10075=="5109"
||issue.customfield_10075=="5112"
||issue.customfield_10075=="5118"
||issue.customfield_10075=="5124"
||issue.customfield_10075=="5128"
||issue.customfield_10075=="5130"
||issue.customfield_10075=="5132"
||issue.customfield_10075=="5133"
||issue.customfield_10075=="5136"))

But, this has not worked. Any Help would be much appreciated. 

Thank You!

Ernesto

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 19, 2020

Hi @Ernesto Donate ,

the logic in your script is slightly incorrect. You want the script to return true when the transition should be allowed, so the first test should be to return true if the issue type is not New Award.

Try this instead:

issue.issueType.name != "New Award" || !["5101","5105","5106","5107","5109","5112","5118","5124","5128","5130","5132","5133","5136"].includes(issue.customfield_10075) || !!issue.customfield_10117 && issue.customfield_10117.some(it => it.value == "X")

This means that the validation will pass if:

  • issue type is not New Award
  • or if it is, then if customfield_10075 is not in the list of values
  • or if it is, if X is selected
Ernesto Donate November 20, 2020

Hello @David Fischer , 

This is been very helpful! I have a few related questions

  • If I want to include more than one Award Type would it be correct to use something like: !["New Award","IssueNameX","IssueNameY"].includes(issue.issueType.name)
  • In general for validators that involve multiple conditions it is correct to use the "build-your-own" option instead of the "Field required Validator" with a condition?

Thanks again!

Ernesto

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 20, 2020

Hi @Ernesto Donate ,

  1. Correct
  2. It really depends. If you're really trying to make a single field mandatory, but only under certain circumstances, it's usually easier to use the Field required (JMWE app) Validator. But in your case, that's not what you're doing, since you want to test the value of customfield_10117, not just whether it is empty or not. In that case, you have no choice but to use a Build-your-own validator.
0 votes
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 2, 2020

I assume you want validation to succeed if the user selects only "None", and fail if he doesn't select anything?

Then try this:

!!issue.customfield_12308 && (issue.customfield_12308.some(it => it.value == "None") && issue.customfield_12308.length == 1 || !issue.customfield_12308.some(it => it.value == "None"))

Which means:

  • customfield_12308 must not be empty, AND:
    • customfield_12308 contains None and no other value, OR
    • customfield_12308 does not contain None

Suggest an answer

Log in or Sign up to answer