Behavior Script to show a text field & make it mandatory upon any change to Multi Checkbox field on the edit screen.
My script is making Text field visible and mandatory upon Multi Checkbox field vale change and if I revert the change on the edit screen, the text field is not hiding
Hi @Kiranped
For your requirement, you will need to use a Server-Side Behaviour configuration for the Checkbox.
Below is an example working code for your reference:-
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours behaviours
def sampleCheckbox = getFieldById(fieldChanged)
def sampleCheckboxValue = sampleCheckbox.value.toString()
def sampleText = getFieldByName('Sample Text')
sampleText.hidden = true
sampleText.required = false
if (sampleCheckboxValue == "Option1") {
sampleText.hidden = false
sampleText.required = true
}
Please note, the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.
I have set the Text Field to be visible in this sample case if only Option1 is selected. If any other options, e.g. Option1 and Option2, are selected, the field is not set to mandatory anymore and is hidden.
Below is a print screen of the Behaviour configuration:-
Below are a few test screens for your reference:-
1) As shown in the print screen below, by default, when the issue is being created, the text field is hidden and not required.
2) Only if Option1 is selected from the Checkbox will the Sample Field appear and be set to mandatory as shown below:-
3) If any other option aside from the Option1 is selected, the Sample Field is set to hidden and not mandatory as shown below:-
4) Steps 1, 2 and 3 also apply to the Edit screen as shown in the following three print screens:-
I hope this helps to answer your question. :)
Thank you and Kind Regards,
Ram
Hi Ram
Thanks for responding.
I am able to show and hide when one particular check box value is selected, but my requirement is when there is a change in the multi-select check box then I need to show the text field and make it mandatory. the moment I revert the changes to the check-box this text field should be hidden.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Kiranped
Could you please provide a print screen to better explain your requirements step-by-step.
Thank you and Kind Regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Ram Kumar Aravindakshan _Adaptavist_ ,
// Get the Original field value of multi-select check box
def fieldName = 'Reason for modifying Product/s Affected'
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjects(underlyingIssue).find{it.name == fieldName}
def outcome = underlyingIssue.getCustomFieldValue(cf).toString()
//Get the current field value selection
def newpfvalue = getFieldById("customfield_15790").getValue()
if(if there is any change in the field value ){
Show text field and make it mandatory
}
else{
hide the text field
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Kiranped
Since you want this to take effect only on the Edit screen, you can try this:-
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours behaviours
def sampleCheckbox = getFieldById(fieldChanged)
def sampleCheckboxValue = sampleCheckbox.value.toString()
def sampleText = getFieldByName('Sample Text')
sampleText.hidden = true
sampleText.required = false
def customFieldManager = ComponentAccessor.customFieldManager
def oldSampleCheckBox = customFieldManager.getCustomFieldObjectsByName('Sample Checkbox').first()
if (underlyingIssue) {
if (underlyingIssue.getCustomFieldValue(oldSampleCheckBox).toString() != sampleCheckboxValue) {
sampleText.hidden = false
sampleText.required = true
}
}
Please note, the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.
Most of the code is the same as the previous example I have provided. The only main difference I have added here is I have included an additional invocation of the checkbox, i.e.:-
def customFieldManager = ComponentAccessor.customFieldManager
def oldSampleCheckBox = customFieldManager.getCustomFieldObjectsByName('Sample Checkbox').first()
and instead of checking the checkbox on a specific value, I am now comparing the existing value of the checkbox when the issue was first created against the updated value using the if condition, i.e.:-
if (underlyingIssue) {
if (underlyingIssue.getCustomFieldValue(oldSampleCheckBox).toString() != sampleCheckboxValue) {
sampleText.hidden = false
sampleText.required = true
}
}
In the Behaviour, the underlyingIssue is used to check for any changes after the issue has been created. Since you want to do this only for the Edit screen, the underlyingIssue will be the suitable approach.
Below are a few print screens for your reference:-
1) When the Edit screen is first opened without any modifications to the checkbox options, the text field doesn't appear.
2) Once the checkbox options are updated, the text field appears
I hope this helps to answer your question. :)
Thank you and Kind Regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Kiranped
Does the answer provide solve your question?
If so, please accept the answer provided.
Thank you and Kind Regards,
Ram
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.