[Behaviours, groovy] Making custom field mandatory, when one of the values in checkbox is selected

Hello,

As in the topic, I am trying to make custom field "FIELD" mandatory, when one of checkbox values is checked. There is one choice, for example: checkbox name: "Find C", values: [A, B, C, D, E, F]; When "C" is selected, "FIELD" becomes mandatory.

I think, I've got a problem with reffering to "C" value.

My code:

def wartosc = getFieldByName("Find C")
def sir = getFieldByName("FIELD")
//def wybor = getFieldByName("C")

if(wartosc.getValue() == "C"){
 sir.setRequired(true)
 }

Might anyone tell me, how can I resolve it? Please, help

2 answers

1 accepted

2 votes

Hi Jan,

You have to setup two scripts, one for the "Find C" field and the other one for "FIELD".

"Find C" script is :

FormField wartosc = getFieldByName("Find C")
FormField sir = getFieldByName("FIELD")
//def wybor = getFieldByName("C")
 
if(wartosc.getValue() == "C"){
   if(sir.getValue() == null || sir.getValue().equals(""))
     sir.setError("Mandatory Field")
} 
else {
    sir.clearError()
}

"FIELD" script is :

FormField sir = getFieldByName("FIELD")
if(sir.getValue() != null && !sir.getValue().equals("")){
 sir.clearError()
}

Please, change Manadatory Field message at your convenience. I just tried this code and it works!

Regards,

Fabio

Thank you Fabio for your answer.

If it really works, I wonder what am I doing wrong:

- my fields are Optional, not Required (btw I checked both possibilites)

- I chose the target project, issue type, workflow, fields and conditions (tranzition for my example)

- I added validation scripts with correct values

.. and still I can see no effect in my screen.

For a better understanding:

//"FIELD" script

FormField sir = getFieldByName("Nr paczki/SIR rewizja")
if(sir.getValue() != null && !sir.getValue().equals("")){
 sir.clearError()
 //sir.setRequired(true)
}

// "Find C" script
FormField wartosc = getFieldByName("Przyczyna powstania blędu")
FormField sir = getFieldByName("Nr paczki/SIR rewizja")
//def wybor = getFieldByName("C")
  
if(wartosc.getValue() == "błąd developmentu"){
   if(sir.getValue() == null || sir.getValue().equals(""))
     sir.setError("Nr paczki/SIR rewizja")
   //  sir.setRequired(true)
} 
else {
    sir.clearError()
   // sir.setRequired(false)
}

Regards,

Jan

Hi Jan,

you should add these scripts into your Behaviour associated with your project.

Please, install and setup this plugin https://marketplace.atlassian.com/plugins/com.onresolve.jira.plugin.Behaviours

I tested my two scripts and they work fine.

Regards,

Fabio

Now it works, thank you! :)

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

256 views 1 18
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you