Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to auto check one option from custom check box field - Service Management

Shah Baloch
Contributor
July 13, 2023

Hi Community,

How can auto-check one option for a custom checkbox field in Service Management? For example, I have a custom check box field and it has three options "Notification1" "Notification2" and "Notification3". I want option three to be selected whenever a customer tries to create a request. The other two options are optional they can select or disregard but one option should be selected all the time. I tried the below behavior short script. It checked the option, but the issue is the other two options are not checkable. Is it possible to have an option auto-select and the other two can be manually selected if needed?

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def notify = getFieldById('customfield_17560')
notify.setFormValue('Notification3')

Thanks,

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Peter-Dave Sheehan
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 14, 2023

There are some recent improvements to behaviours that I've not yet experimented with that might make this simpler.

But in older versions, you have to use the option id with setFormValue on Select and Checkbox fields. And for checkbox and multiselect, you have to offer an array of optionId.

Try this script:

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def notifyFormField = getFieldById('customfield_17560')
def notifyCf = ComponentAccessor.customFieldManager.getCustomFieldObject(notifyFormField.fieldId)
def config = notifyCf.getRelevantConfig(issueContext)
def options = ComponentAccessor.optionsManager.getOptions(config)
notifyFormField.setFormValue([options.find{it.value == 'Notification3'}.optionId])
Shah Baloch
Contributor
July 14, 2023

Hi @Peter-Dave Sheehan it works, I'm seeing the third option is checked when I try to create a request, however, the only issue is that I can't check the other two options manually. The goal is that 3rd option should be always auto check and the user should be able to check the other two options manually. Any idea why it's not letting me check the other two options?

Peter-Dave Sheehan
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 14, 2023

Did you toggle the "read-only/editable" switch on the behaviour configuration?

Can you share a screenshot of the behaviour screen?

Shah Baloch
Contributor
July 14, 2023

@Peter-Dave Sheehanmy bad I was adding code on server-side-script for the notify custom field. I removed the code from notify field and added code on Initialiser and it worked.

Now I'm trying to display an error message if someone uncheck the option. I added the below code, the issue is when the page loads the error is already there. If uncheck and check it back then the error goes away. I added the if condition part in the server-side script but nothing got changed. Is it possible to add the error if someone uncheck the option?

if(notifyFormField.value != 'Notification3') {
    notifyFormField.setError('Notification3 option is mandetory, it has to be check in order to create a request. The other two options are optional.')
}
else {
    notifyFormField.clearError()
}
Thanks
Peter-Dave Sheehan
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 14, 2023

Put the script to set the default value in the Initializer.

Then, add the script to display the error in the field's server-side script.

But you need to check that 1 or the values are Notification3.

Try something like this:

def notifyFormField = getFieldById('')
notifyFormField.clearError()

def notfiyValues = (notifyFormField.value ?: []) as List
if
(!notfiyValues.contains('Notification3') {
    notifyFormField.setError('Notification3 option is mandetory, it has to be check in order to create a request. The other two options are optional.')
}
Like Shah Baloch likes this
Shah Baloch
Contributor
July 14, 2023

It worked, thank you so much @Peter-Dave Sheehan 

0 votes
Manoj Gangwar
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 14, 2023

Hi @Shah Baloch , 

Under field configuration, You can make the field value as a default so it will be always checked. 

Shah Baloch
Contributor
July 14, 2023

Hi @Manoj Gangwar, I already tried that, but the issue is it leaves the option checked from the backend of the service desk, which we don't want since sometimes we create tickets using the backend for internal tasks. We just wanted to check for the portal I thought it might be achieved using Scriptrunner.

TAGS
AUG Leaders

Atlassian Community Events