I need help to write a script please. make one field required based on another field's value

I have seen many examples, but none of them work in my jira.

both fields are customfields.

the field I am checking is a checkbox, yes/no

if set to yes, I want a text field to be required.

both fields are on a screen that pops up when I click on the done transition.

thankyou for your help with this.

5 answers

2 accepted

0 vote

Hello, 

You should be able to accomplish this with a behaviour. A behaviour should be pretty simple as long as I'm understanding the screen you're using it on correctly (a workflow transition screen).

Some simple psuedo code for you:

// get the checkbox, getFieldById(getFieldChanged()) or getFieldById("customfieldId")
// get the other field, getFieldById(customfieldId)

// if checkbox.getValue() as String == 'yes'
// then set the other custom field to required, otherField.setRequired(true)

If you're setting the checkbox value in a preivous workflow step, you should use an initializer function, if not you should use a field behaviour set up to work on the checkbox field. 

If you need further help setting this up, let me know. :)

Jenna

Jenna Davis Community Champion Jul 17, 2017

You should also be able to set up a simple scripted validator using something similar to these examples

Either method should work, it is just based on your preference and whichever you find easier to set up and maintain. :)

Hope this helped you out!

Jenna

it doesn't do anything and I see this error in the validator dialogue

 

Cannot get property 'customfield_12718' on null object

What is the exact code you're using and where? 

weird.  I posted it, but it didn't show up here.  I have this code setup in a validator in my workflow on my done transition.

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

FormField checkbox = getFieldById("customfield_12718")
FormField otherfield = getFieldById("customfield_12719")

if (checkbox.getValue() as String == 'yes') {
 otherField.setRequired(true)
}

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

FormField checkbox = getFieldById("customfield_12718")
FormField otherfield = getFieldById("customfield_12719")

if (checkbox.getValue() as String == 'yes') {
 otherField.setRequired(true)
}

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

FormField checkbox = getFieldById("customfield_12718")
FormField otherfield = getFieldById("customfield_12719")

if (checkbox.getValue() as String == 'yes') {
 otherField.setRequired(true)
}

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

FormField checkbox = getFieldById("customfield_12718")
FormField otherfield = getFieldById("customfield_12719")

if (checkbox.getValue() as String == 'yes') {
otherField.setRequired(true)
}

Okay, the code you're using would probably work if you were using a Behaviour. Behaviours have some slightly different methods than everything else and have to have a proper setup and mapping applied to them to work. As a validator it is not going to work though, that is why you are getting errors.

If you want to do that you need to go to Behaviours. Then click "Mapping" and map it to appropriate the project/issue-type etc., and then click "Fields" and add an Initialiser.

You do not have to do this if you think setting up a validator would be easier and still achieve what you need though.

What type of validator are you setting up (a simple scripted validator, a custom scripted validator, etc.)? You need to make a simple scripted validator. There should be an option when you click 'Script Validator' to select this.

Please try this code inside of your validator if you want to that:

cfValues['Checkbox Name']*.value.contains("yes") || cfValues['Text Field Name']


Also, I would like to make the suggestion that you switch to using radio buttons instead of checkboxes unless you need users to have the option to select multiple fields. If you're using a validator.

If you switch to radio buttons you should be able to use this code: 

cfValues['Checkbox Name']?.value == 'yes' || cfValues['Text Field Name']

ty for pointing out the behaviors thing.  I had no idea what people were talking about until you said this in the way you said it.  Then I realized that I needed to use the addon screen to see behaviors.  I am trying to set it up that way now.

Jenna Davis Community Champion Jul 18, 2017

No problem! If you have any trouble getting it working let me know. 

Jenna

How about a validator in the workflow? 

We also use "Automation for Jira" which allows for If > Then statements that could accomplish a similar function. 

Yes, I want a validator in the workflow.  I just need help with the code.  I have tried a bunch of things I have found on the network like,

issue.resolutionObject.name != "Fixed" || issue.fixVersions

 

 

nothing I have tried works. 

Mine might be part of the JWME addon ........

Try Validator > Regular Expression > Choose field then set expression to somehting like Option [yes]

thank you, I have that one. but

then what?  how do I make the other field mandatory?

You do the same thing for the second field. If the issue doesnt meet the criteria when you try to transition it will spit out an error instead. 

I have tried the following:  Add validator -> choose script validator, then entered this code.

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

FormField checkbox = getFieldById("customfield_12718")
FormField otherfield = getFieldById("customfield_12719")

if (checkbox.getValue() as String == 'yes') {
 otherField.setRequired(true)
}

 

I think the problem with the examples is that they are not complete code.  the snippets don't tell me what else I need to make them work.

 

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
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

356 views 0 6
Read article

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