How to restrict users adding additional labels?

Shah Baloch
Contributor
May 30, 2024

Hi Community,

I am trying to create a Behaviour script to not allow the users to add additional values in a custom label field. When the user creates a ticket, it shows three suggestions: Teams, Email, and Phone. I want users only to be able to select these options. If the user tries to add a new option, it should display an error. I am trying to make the below script work but it's not working. Adding this script also caused another script to stop working (The project has another script that was created a couple of years ago). I didn't want to touch the existing one, so I created a separate Behavior. Neither of the scripts works if I add the below script to the server-side script field for the Notification Method custom field. Can someone help me fix the script, or do you have any other script that I can use to achieve this?

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor
class CustomFieldBehavior extends FieldBehaviours {

    @Override
    void setUp() {

        // Initialize custom field
        FormField notificationMethod = getFieldById("customfield_21101")
 
        // Predefined options
        def allowedValues = ["Teams", "Email", "Phone"]      

        // Get the selected values
        def selectedValues = notificationMethod.getValue() as List<String>

        // Check if all selected values are in the allowed values list
        def invalidValues = selectedValues.findAll { it !in allowedValues }       

        if (invalidValues) {
            notificationMethod.setError("Invalid selection: ${invalidValues.join(', ')}. Please select from available options ${allowedValues.join(', ')}.")
            notificationMethod.setFormValue(selectedValues.findAll { it in allowedValues })  // Remove invalid selections

        } else {
            notificationMethod.clearError()
        }
    }
}

// Registering the behavior for the specific field
def behavior = new CustomFieldBehavior()
behavior.setUp()

Appreciate your help!

2 answers

1 vote
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.
June 4, 2024

Hi @Shah Baloch

For your requirement, I suggest using ScriptRunner's Server-Side Behaviour for the Label field.

You can try something like this:-

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def labels = getFieldById(fieldChanged)
def labelsValue = labels.formValue as List<String>

def permittedLabels = labelsValue in ['Sample1','Sample2','Sample3']

labels.clearError()

if ( labelsValue.size() > 3 || !permittedLabels) {
labels.setError('No more labels allowed')
}

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the Behaviour configuration:-

behaviour_config.png

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram

Shah Baloch
Contributor
June 4, 2024

Hi @Ram Kumar Aravindakshan _Adaptavist_ it's interfering with an existing script that I stated earlier. I mean, when I added your provided script and  tried to create a ticket, it displayed an error message from the right side of the page (Error from the right side of screenshot). However I disabled the existing script to test the new one, that error goes away, but it won't allow me to create a ticket and keep showing the error "No more labels allowed." This not allowed error not go away regardless if I select an existing option from the list that I added to the script or try to create a new label. Thank you,

scriptrunner_06042024.png

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.
June 4, 2024

Hi @Shah Baloch

Firstly, I don't understand why you must create a custom class for this Behaviour.

Your approach can cause a lot more complications.

It would be best not to use the simplified approach that I have provided.

Alternatively, if the Behaviour is not working as expected, you can use the Custom Script Validation, which will check the content of the Label field before permitting the issue to be created.

I'll provide a sample once I get the time.

Thank you and Kind regards,
Ram

 

Like Shah Baloch likes this
Shah Baloch
Contributor
June 5, 2024

Hi @Ram Kumar Aravindakshan _Adaptavist_ I made some changes and was able to make it work but the only issue is it only work if select more than one option, for example two of those option or three, I select only one option "Email" it shows an error "No more than 3 labels are allowed."  

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours behaviours

def labels = getFieldById(fieldChanged)
def labelsValue = labels.formValue as List<String>

// Define the list of permitted labels
def permittedLabels = ['Teams', 'Email', 'Phone']

// Clear any previous errors
labels.clearError()

// Check if any labels are invalid (i.e., not in the permitted list)
def invalidLabels = labelsValue.findAll { !(it in permittedLabels) }

// Check if there are more than 3 labels or any invalid labels
if (labelsValue.size() > 3) {
    labels.setError('No more than 3 labels are allowed.')

} else if (!invalidLabels.isEmpty()) {
    labels.setError('Only Teams, Email, and Phone are permitted.')
}

Do you know why it's not working with a single selection? The goal is either one of these option is select or two or all three are selected it should not display an error. Thanks,

Screenshot 2024-06-05 155208.pngScreenshot 2024-06-05 155240.png

0 votes
karim -Atlassway-
Banned
June 5, 2024

Hi @Shah Baloch 

Try out  Colored label Manager  , you have the ability to restrict new label items creation, Project Admins can add items to the field. User can select those items from the list. Useful for label fields like Definition of Done or Select List .

You can also report on labels , there’s absolutely no coding required and certainly no need to learn Jira Query Language (JQL) for that matter.

And more ..

 

Atlassway Team

 

Shah Baloch
Contributor
June 5, 2024

@karim -Atlassway- We try our best not to add additional add-ons. Script runner is a big help. Also your plugin is for Cloud and we are using data center. Thanks

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events