Forums

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

How to make a field mandatory based on value in the another field

Anirudh Pratap Verma April 5, 2023

Hi All,

 

I am trying to make a field mandatory based on value in the another field using below code in script runner. But it is not working 100% correctly -

- When i create a JIRA ticket and make the select 1 in the field "Quality Assessment", it doesnt show "Quality Comment" field as mandatory. We have to click on update and again edit the JIRA ticket to see the field "Quality Comment" as mandatory. 

- Also it doesnt stop state transition even when the field is made mandatory and not filled. 

 

The only thing it does is that it doesnt allow to edit the JIRA till the time we fill the mandatory field field "Quality Comment".

 

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

@BaseScript FieldBehaviours behaviours

def sourcefield = getFieldByName('Quality Assessment')
def RCAfield = getFieldByName('Quality Comment')

String sourceValue = sourcefield.getValue()

if(sourceValue.startsWith("1")){
RCAfield.setRequired(true);
} else {
RCAfield.setRequired(false);
}
How can I resolve our two problems mentioned above. Even if we are able to solve the second problem of transitioning then also i am okay.
Thanks, 

 

1 answer

0 votes
Benjamin
Community Champion
April 5, 2023

Hi @Anirudh Pratap Verma ,

 

Based on your script, the values will only take place after an edit which is the expected behavior you have. You'll need to have a line above to check for when field are change on the screen to check against the value. Here's the example line:

def sourcefield = getFieldById(getFieldChanged())

 

Would not recommend using start with 1. If you know the value is 1, would state it with an = statement.

 

-Ben

Anirudh Pratap Verma April 6, 2023

Thanks Ben for your response!!

I put the below code in the server side script for a the custom field which is responsible for  change in the below "Quality Comment" and it worked.

 

For only below two options field should display and show as mandatory. 

 

def QACommentfield = getFieldByName("Quality Comment")
def failReasonfield = getFieldById(getFieldChanged())

def selectedoption1 = failReasonfield.getValue() as String
def selectedoption2 = failReasonfield.getValue() as String
def isOptionSelected1 = selectedoption1 == "1 - Alarming"
def isOptionSelected2 = selectedoption2 == "2 - Warning"

QACommentfield.setHidden(! isOptionSelected1 && ! isOptionSelected2)
QACommentfield.setRequired(isOptionSelected1 || isOptionSelected2)

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
9.4.0
TAGS
AUG Leaders

Atlassian Community Events