Making a fields mandatory depending on other field.

Rajiv Ranjan April 9, 2019

We want to set a automate producer where one fields get filled others 2 fields will be mandatory or vice-versa.

Can we set a rule so that one of the 3 fields (Wiser Case #, Task ID, Customer Name(s)) is populated all others will be required as well?

1 answer

0 votes
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 9, 2019

Hi @Rajiv Ranjan ,

This is possible indeed. Add a behaviour script to each one of these fields. For example on field 1, you would use this : 

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

@BaseScript FieldBehaviours fieldBehaviours

def cf1_id = 10100
def cf2_id = 10101
def cf3_id = 10102

def cf1 = getFieldById("customfield_"+cf1_id)
def cf2 = getFieldById("customfield_"+cf2_id)
def cf3 = getFieldById("customfield_"+cf3_id)

def cf1Value = cf1.getValue()

if (cf1Value != null){
cf2.setRequired(true)
cf3.setRequired(true)
}

Of course you can change the condition.

Antoine

Rajiv Ranjan April 9, 2019

Hello @Antoine Berry ,

Thank you for the code and suggestion!!!

Update the code you provided with the respective customfield_id and added the code to a server-side script to the "Wiser Case#" field i.e. first field however while creating the ticket others two fields are mandatory without putting any value to the first field which is wrong.

I need after putting some value to the first field others two fields should be mandatory.

Rajiv Ranjan April 9, 2019

image.pngimage.pngAttached the Behaviour setting and create screen

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 10, 2019

Ah, you just need to tweak the behaviours a bit. First I would suggest to make the fields optional in the field configuration. Then just add an "else" statement : 

 

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

@BaseScript FieldBehaviours fieldBehaviours

def cf1_id = 10100
def cf2_id = 10101
def cf3_id = 10102

def cf1 = getFieldById("customfield_"+cf1_id)
def cf2 = getFieldById("customfield_"+cf2_id)
def cf3 = getFieldById("customfield_"+cf3_id)

def cf1Value = cf1.getValue()

if (cf1Value != null){
cf2.setRequired(true)
cf3.setRequired(true)
}
else {
cf2.setRequired(true)
cf3.setRequired(true)
}

Antoine

Rajiv Ranjan April 10, 2019

Hi @Antoine Berry 

 

The fields are optional but different types.

As suggested after checking the fields configuration I have inserted the new code however the same result. Without putting any value to 1st field the other two fields are mandatory.

Later on, updated the code as its look like returning the same value in If or else condition.

{code}

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

@BaseScript FieldBehaviours fieldBehaviours

def cf1_id = 10001
def cf2_id = 10002
def cf3_id = 10003

def cf1 = getFieldById("customfield_"+cf1_id)
def cf2 = getFieldById("customfield_"+cf2_id)
def cf3 = getFieldById("customfield_"+cf3_id)

def cf1Value = cf1.getValue()

if (cf1Value != null){
cf2.setRequired(true)
cf3.setRequired(true)
}
else {
cf2.setRequired(false)
cf3.setRequired(false)
}

{code}

Please assist here.

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 11, 2019

Hi @Rajiv Ranjan ,

Maybe that is because the cf1Value is not null even if the field is empty (if it is a text field for example). You need to test the script (with logs) for each field to make sure this is working the way you intend.

Try to update the script to : 

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

@BaseScript FieldBehaviours fieldBehaviours

def cf1_id = 10001
def cf2_id = 10002
def cf3_id = 10003

def cf1 = getFieldById("customfield_"+cf1_id)
def cf2 = getFieldById("customfield_"+cf2_id)
def cf3 = getFieldById("customfield_"+cf3_id)

def cf1Value = cf1.getValue()

log.error("cf1Value : " + cf1Value)
log.error("cf1Value class : " + cf1Value.getClass())

if (cf1Value != null && cf1Value != ""){
cf2.setRequired(true)
cf3.setRequired(true)
}
else {
cf2.setRequired(false)
cf3.setRequired(false)
}

Antoine

Rajiv Ranjan April 16, 2019

Hi Antoine,

Since it is multi-text field  cf !="" works.

Now if part working fine but else part when we clear Task Id (Text Field (single line))value the Wiser_Case# does not marked optional 

//Get a FormField object to retrieve the value.
def Wiser_Case_No = getFieldById("customfield_11101")
def Customer_Names = getFieldById("customfield_10804")
def Task_ID = getFieldById("customfield_10600")

//Gets the value from the field
def Wiser_Case_NoValue = Wiser_Case_No.getValue()
def Task_IDValue = Task_ID.getValue()

//Error log
log.error("Task_IDValue : " + Task_IDValue)
log.error("Wiser_Case_NoValue : " + Wiser_Case_NoValue)
//If Wiser Case# not empty or Task ID not empty then Customer name is mandatory.

if ((Task_IDValue != "") || (Wiser_Case_NoValue != ""))
{
Customer_Names.setRequired(true)
}
else
{
Customer_Names.setRequired(false)
}

//If task ID is not empty then Wiser Case # is mandatory
if (Task_IDValue != "")
{
Wiser_Case_No.setRequired(true)
}
else
{
Wiser_Case_No.setRequired(false)
}

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 16, 2019

Hi @Rajiv Ranjan ,

What custom field type is Task ID ? Maybe you need to compare against null instead of "" (if it is a number for example). Otherwise it looks fine :)

Antoine

Rajiv Ranjan April 16, 2019

It's "Text Field (single line)" 

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 16, 2019

At first glance it should work with text field. What do you get in the logs for "Task_IDValue" ?

Rajiv Ranjan April 16, 2019

Hi Antoine,

 

Separate the behavior script for each of the fields, now it working as expected.

 

Thank you so much for your guidance. 

Like Antoine Berry likes this
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 16, 2019

Glad to help ! Indeed, you need to map the script to the field that will "listen" (i.e. trigger) to it. Please consider accepting the answer if you are satisfied with it. :)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events