Hello everyone,
I have 2 custom fields (Select List (multiple choices)) which I have already made mandatory via validators for the Create Screen, if a value (or multiple) for 1 field will be selected the other field will disappear and will no longer be mandatory of course.
Problem is that if a user Edits the issue these 2 fields are not mandatory and both values can be set to "None".
I want to use Behaviours for the Edit Screen part, make both fields mandatory and have the same results as above, if I select a value for 1 field the other field will disappear and will no longer be mandatory.
Any idea on how to achieve this?
Thank you!
Hello @Ciocirlan, Costel-Iulian ;
You would need to create a behaviour for each field. You could use the same script for each behaviour for more convenience, such as :
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def field1 = getFieldById("customfield_13000")
def field2 = getFieldById("customfield_13001")
def field1Value = field1.getValue()
def field2Value = field2.getValue()
if (field1Value.contains("value 1")){ field2.setRequired(true); field2.setHidden(false);}
if (field1Value.contains("value 2")){ field2.setRequired(false); field2.setHidden(true);}
if (field2Value.contains("value 3")){ field1.setRequired(true); field1.setHidden(false);}
if (field2Value.contains("value 4")){ field1.setRequired(false); field1.setHidden(true);}
Update it with your IDs / values. You would need to adapt it since you are using multi select lists. What happens if the user selects two values that should both hide and make the other field mandatory ?
Hello Antoine,
Thank you for the reply!
Yes, the fields are called To VG & To MXA. So, if I am to select 1 or multiple values from To VG then To MXA will no longer be visible/mandatory. Same for the To MXA field, just the other way around.
I will check your solution, thank you again!
Costel
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm not going to post a separate response because I think Antoine was essentially correct, but I though you might appreciate a second perspective.
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def vgFld = getFieldById("customfield_13000")
def mxaFld = getFieldById("customfield_13001")
def changedFld = getFieldById(fieldChanged)
def vgValue = vgFld.value
def mxaValue = mxaFld.value
def hideMxa = vgValue && vgFld == changedFld
def hideVg = mxaValue && mxaFld == changedFld
mxaFld.setHidden(hideMxa).setRequired(!hideMxa)
vgFld.setHidden(hideVg).setRequired(!hideVg)
if(hideMxa){
mxaFld.setFormValue(null)
}
if(hideVg){
vgFld.setFormValue(null)
}
Couple things I wanted to highlight...
1) you can string multiple behaviour methods togeter... instead of field1.setHiden(true); field1.setRequired(false), you can combine as field1.setHidden(true).setRequired(false).setFormValue() etc
2) You probably want to clean up the field when you hide it... otherwise your data will be unreliable. In this case it may not matter. If the script runs correctly, you will never be able to have data in both fields at once, but it's good practice to clear the field when you hide it.
3) I added a changedFld variable to check which field is currently being changed. And use that as a way to decide what to do to the other field. You have to make sure that the code will UNHIDE the other field too is you end up clearing the value.
The line
def hideMxa = vgValue && vgFld == changedFld
will only be true IF VG contains a value (not counting "none") and the VG field was just modified. If it's false (either because the field didn't, or was just changed to none), then we will unhide MXA. And vice versa.
As Antoine said, you put this as the server-side script for both fields, and changing each field will trigger the visibility on the other.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Antoine & Peter,
Thank you both for the help! I managed to make it work :D
Have a nice day ahead!
Costel
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.