Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

I want to make two simple text fields required when the value of "Regression" field is set to "Yes"

tomh June 12, 2023

Upon Bug creation:
When "Regression" value is set to "Yes"
Make the fields "Good version" "Bad version" required

1 answer

1 accepted

0 votes
Answer accepted
Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 12, 2023

Hi, @TomH 

If you have ScriptRunner, you can set it with Behaviours.

tomh June 12, 2023

Yes, i am using script runner at the moment and i managed to get it now to make the fields throw an exception when they are empty but for some reason it is also throwing when i add values to the required fields

Here is the code im using:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.opensymphony.workflow.InvalidInputException

def issues = issue as MutableIssue

// Field names
def regressionFieldName = "Regression"
def goodVersionFieldName = "Good version"
def badVersionFieldName = "Bad version"

// Get field objects
def customFieldManage = ComponentAccessor.getCustomFieldManager()
def regressionField = customFieldManager.getCustomFieldObjects(issue).find { it.name == regressionFieldName }
def goodVersionField = customFieldManager.getCustomFieldObjects(issue).find { it.name == goodVersionFieldName }
def badVersionField = customFieldManager.getCustomFieldObjects(issue).find { it.name == badVersionFieldName }

// Get field values
def regressionValue = issue.getCustomFieldValue(regressionField) as String
def goodVersionValue = issue.getCustomFieldValue(goodVersionField) as String
def badVersionValue = issue.getCustomFieldValue(badVersionField) as String

// Check if Regression field is set to "Yes"
if (regressionValue == "Yes") {
// Check if Good version field is empty or null
if (goodVersionValue?.trim()?.isEmpty()) {
throw new InvalidInputException("Good version is required when Regression is set to Yes.")
}
// Check if Bad version field is empty or null
if (badVersionValue?.trim()?.isEmpty()) {
throw new InvalidInputException("Bad version is required when Regression is set to Yes.")
}
}
Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 12, 2023

Looks like I understood the problem.

You have to split behaviour in two parts. I made an example. In example imagine, that my field "Self Development" is your "Regression"

image.png

Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 12, 2023
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours
FormField gvField = getFieldByName("Good Version")
FormField bvField = getFieldByName("Bad Version")
gvField.setHidden(true)
bvField.setHidden(true)

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

FormField affectedField = getFieldById(getFieldChanged())  // Lets imagine it's Regression CF
def affectedVal = affectedField.value
FormField gvField = getFieldByName("Good Version")
FormField bvField = getFieldByName("Bad Version")

if (affectedVal == "Yes") {
    gvField.setHidden(false)
    bvField.setHidden(false)
    gvField.setRequired(true)
    bvField.setRequired(true)
} else {
    gvField.setHidden(true)
    bvField.setHidden(true)
    gvField.setRequired(false)
    bvField.setRequired(false)
}
tomh June 12, 2023

First of all, thanks a lot for the fast replies!
 

Where did you use this code? Im using "Simple scripted validator" It looks like you are using some other validator type maybe?
Is that in the custom field configuration?
Thanks

Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 12, 2023

It's not vaidator. It's behaviour. It's used, for example, to prefill description, on issue creation (and many other cases)
It's located here:

https://your.address/plugins/servlet/scriptrunner/admin/behaviours

You wrote, that on creation you want users to fill required fields - fine, use behaviours :)

How it works - you create new behaviour, map it with needed project and issue type, then add there scripts, which I wrote. That's all, no validators needed.

Take a look, they are very cool in cases, when you want to automate actions, while user uses issue creat/edit screens.
https://docs.adaptavist.com/sr4js/latest/features/behaviours/behaviours-tutorial

tomh June 12, 2023

OMG, thank you, i managed to make it work via script runner behaviours like you said.

Thanks a lot for the help

Like • Evgenii likes this
tomh June 12, 2023 edited

All works as expected. Thanks

Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 12, 2023 edited

Well, why not. Let's try zoom.

You need to look at changes of "Regression" field, not "Good version" or "Bad version".

You can log events with log.warn("Something to log: ${resultOfAction}")

Suggest an answer

Log in or Sign up to answer