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

How to make both select list is required?

Егор July 3, 2023

I have created cascading list with two dropdown lists, which created by Dynamic Forms Jira Plugin. But if I fill parent list and not fill child list - my issue anyway create. How I can make both fields of Dynamic Cascading Select are required to prevent the task from being created if the inherited field is empty.

2 answers

1 accepted

2 votes
Answer accepted
Kseniia Trushnikova
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 3, 2023 edited

@Егор, привет!

Here's the example for Scripted (Groovy) Validator (JMWE app):

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

Issue myIssue = issueObject
CustomField cascadingCF = customFieldManager.getCustomFieldObjectByName('YOUR_FIELD_NAME')
Map cfVal = myIssue.getCustomFieldValue(cascadingCF) as Map
String first = cfVal.get(null)
String second = cfVal.get("1")

if (second != null)
return true
else
return false 

 

Егор July 3, 2023

@Kseniia Trushnikova привет!

Спасибо, завтра испробую.

Егор July 3, 2023

@Kseniia Trushnikova спасибо! Всё отлично работает :)

0 votes
Markus Fredén
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 3, 2023

Hi @Егор ,

To make both fields of the Dynamic Cascading Select required, you can use a combination of JavaScript validation and Jira's built-in field configuration options. Here's how you can achieve this:

1. Enable the JavaScript validator in Jira:
- Go to Jira Administration.
- Navigate to Issues > Custom Fields.
- Find the Dynamic Cascading Select field and click on "Configure" next to it.
- Under the "Options" section, enable the "Use JavaScript Validator" option.

2. Implement JavaScript validation for the cascading select fields:
- In the JavaScript Validator textarea, write the following code:

function validateCascadingSelect(value, field, issue, errors) {
var parentFieldId = "customfield_XXX"; // Replace XXX with the ID of the parent field
var childFieldId = "customfield_YYY"; // Replace YYY with the ID of the child field

var parentFieldValue = issue.fields[parentFieldId];
var childFieldValue = issue.fields[childFieldId];

if (!parentFieldValue || !childFieldValue) {
errors.push(field.name + " is required.");
}
}

- Replace "customfield_XXX" and "customfield_YYY" with the actual IDs of your parent and child fields, respectively.

3. Save the JavaScript validation code and configure the field:
- Click on the "Update" button to save the JavaScript validation code.
- Make sure the cascading select field is marked as required in the Jira field configuration.

With this setup, when a user tries to create or edit an issue, the JavaScript validator will check if both the parent and child fields have values. If any of them is empty, an error message will be displayed, and the issue will not be created or updated until both fields have valid values.

Obviously, You should test this in a test environment before implementing in Production.

/Markus

Егор July 3, 2023

Hi @Markus Fredén 

JS Validator - its plugin or extension for Jira?
I opened System - Issues - Custom Fields - Configure(my dynamic cascading select). But haven't "Use JS Validation" option
image.png

Егор July 3, 2023

@Markus Fredén 
I added validation:


function validateCascadingSelect(value, field, issue, errors) {
var parentFieldId = "customfield_15150"; // Replace XXX with the ID of the parent field
var childFieldId = "customfield_15150-1"; // Replace YYY with the ID of the child field

var parentFieldValue = issue.fields[parentFieldId];
var childFieldValue = issue.fields[childFieldId];

if (!parentFieldValue || !childFieldValue) {
errors.push(field.name + " is required.");
}
}


But when I trying to create issue I have an error:
image.png

Steps to reproduce:
1. Go to Issues - Workflows - *my_workflow* 
2. Click on step Create - To open
3. Add Validator
4. Scripted (Groovy) Validator (JMWE app) (radio button)
5. Add your code block with change values of id
6. Update
7. Create new task

Markus Fredén
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 3, 2023

Hi @Егор ,

My apologies, I was assuming that you had ScriptRunner installed (I'm so used to it) which provides the JS validator feature.

/Markus

Markus Fredén
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 3, 2023 edited

Since you have JMWE and not SR you can use the workflow function called "Jira Expression" which accepts JavaScript logic, for example:

!issue.get("customfield_1") || !issue.get("customfield_2")

This expression checks if either of the custom fields (customfield_1 and customfield_2) is empty. If any of the fields is empty, the expression will evaluate to true and the validator will fail.

/Markus 

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, ace, atlassian community event, donation, girls who code, women in tech, malala fund, plan international, kudos, community badge, badge, atlassian badge, International Women’s month, International Women’s Day, women's month, women's day

10 for Change at Atlassian Community Events

Show up and give back by attending an Atlassian Community Event: we’ll donate $10 for every event attendee in March!

Join an Atlassian Community Event!
AUG Leaders

Upcoming Jira Events