Behavior Script to show a text field & mandatory upon any change to Multi Checkbox on edit screen

Kiranped December 9, 2021

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

 

 

1 answer

1 accepted

2 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 11, 2021

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:-

behaviour_config.png

 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.

image1.png

2) Only if Option1 is selected from the Checkbox will the Sample Field appear and be set to mandatory as shown below:-

image2.png

3) If any other option aside from the Option1 is selected, the Sample Field is set to hidden and not mandatory as shown below:-

image3.png

4) Steps 1, 2 and 3 also apply to the Edit screen as shown in the following three print screens:-

image4.png

 

image5.png

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

Kiranped December 13, 2021

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.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 13, 2021

Hi @Kiranped

Could you please provide a print screen to better explain your requirements step-by-step.

Thank you and Kind Regards,

Ram

Kiranped December 13, 2021

@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

}

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 20, 2021

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.

original_choice.png

 

2) Once the checkbox options are updated, the text field appearsupdated_choice.png

 

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 9, 2022

Hi @Kiranped

Does the answer provide solve your question?

If so, please accept the answer provided.

 

Thank you and Kind Regards,

Ram

Suggest an answer

Log in or Sign up to answer