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

Marlene Cote July 13, 2017

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 votes
Answer accepted
Marlene Cote July 17, 2017

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

 

Cannot get property 'customfield_12718' on null object

Jenna Davis
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 17, 2017

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

Marlene Cote July 17, 2017

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)
}

Marlene Cote July 17, 2017

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)
}

Marlene Cote July 17, 2017

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)
}

Marlene Cote July 17, 2017
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)
}
Jenna Davis
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 18, 2017

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']
Marlene Cote July 18, 2017

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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 18, 2017

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

Jenna

0 votes
Answer accepted
Jenna Davis
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 17, 2017

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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 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

0 votes
Marlene Cote July 17, 2017

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.

 

0 votes
Marlene Cote July 17, 2017

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)
}

 

0 votes
Shawn Masters
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 13, 2017

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. 

Marlene Cote July 13, 2017

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

 

 

Marlene Cote July 13, 2017

nothing I have tried works. 

Shawn Masters
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 13, 2017

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

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

Marlene Cote July 13, 2017

thank you, I have that one. but

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

Shawn Masters
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 13, 2017

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. 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events