Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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

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.

Hi @Kiranped

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

Thank you and Kind Regards,

Ram

@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

}

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

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
TAGS
Community showcase
Published in Jira Software

👋 Looking for 15-20 volunteers to test Atlassian training content

Hi everyone! Are you interested in beta testing Atlassian University’s newest (unreleased!) training course? We’re looking for 15-20 volunteers to test our newest training course, Basic reporting...

628 views 21 25
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you