ScriptRunner Assistance for Custom Jira Field Validation

TheAsh February 16, 2024

I have a custom field called 'OS' (field ID 2354); it is a single-select field with multiple choices containing values such as 'Ubuntu,' 'RHEL,' and 'Windows,' which determine the operating system for my Jenkins job.
I now require a script that allows users to select values, but specifically prevents the selection of 'Windows' in conjunction with other values. An error should be triggered when attempting to create or update a ticket under this condition.

I attempted this using ScriptRunner Behaviour without success. Can anyone provide assistance?

 

Refined Conditions:

  • If I select a single value, such as Ubuntu, RHEL, or Windows, then I should be able to create or update a Jira issue.
  • If I select multiple values, such as Ubuntu and RHEL, then I should be able to create and update a Jira issue.
  • If I select combinations like "Ubuntu & Windows," "RHEL & Windows," or "Ubuntu, RHEL & Windows," then I shouldn't be able to create or update the Jira issue. Instead, a message should be displayed, stating: "Windows cannot be selected with other values."

3 answers

2 accepted

1 vote
Answer accepted
Ashok Barik February 19, 2024

Hi @TheAsh , 

You can achieve this by creating a simple scripted validator in the workflow.

Open the Respected Workflow:

  • Navigate to the workflow that requires validation.

Edit the Workflow:

  • Click on the "Edit" option to make changes to the workflow.

Select the Desired Transition:

  • Identify and choose the transition where you want to apply the validator.

Click Validator:

  • Locate the option for adding validators and click on it.

Add Validator:

  • Choose to add a validator to the selected transition.

Select "Simple Scripted Validator" [ScriptRunner]:

  • From the available options, choose "Simple Scripted Validator" provided by ScriptRunner.

Configure Condition Field:

  • In the condition field, input the following script:
!('Windows' in cfValues['OS']*.value && ('Ubuntu' in cfValues['OS']*.value || 'RHEL' in cfValues['OS']*.value))

Enter Validation Message:

  • Specify the message you want to display when the validation condition is not met.

Select Corresponding Field:

  • Choose the field for which the validation applies. In this case, select the "OS" field.


Please refer to the attached screenshot for guidance. Note that in the example, I have selected the field "Cluster"; however, in your case, replace it with the field "OS."

ScriptRunner.png

TheAsh February 19, 2024

Thank you @Ashok Barik . 

1 vote
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.
February 16, 2024

Hi @TheAsh

In your description, you mentioned:-

I now require a script that allows users to select values, but specifically prevents the selection of 'Windows' in conjunction with other values. An error should be triggered when attempting to create or update a ticket under this condition.

So, from a first level of understanding, you want to filter the options on the OS list based on the option selected from another field.

Or do you intend to filter the options available in the OS field based on the User's role or Group?

If it's the former, I have provided a similar solution in this Community Post as well as this Community Post.

If it's the latter, please let me know based on what condition you intend to filter the OS list. 

I am looking forward to your feedback.

Thank you and Kind regards,

Ram

 

TheAsh February 16, 2024

Hi @Ram Kumar Aravindakshan _Adaptavist_ Thank you for looking into my query. I have just updated and refined the conditions. Could you please review them and let me know if you need any additional information from my side?

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.
February 16, 2024

Hi @TheAsh

Could you please share the updated code so I can review it?

Thank you and Kind regards,

Ram

TheAsh February 16, 2024

Hi @Ram Kumar Aravindakshan _Adaptavist_ Please check the following code what I have used in Behaviours , but it is useless for me.  It doesn't work while creating an issue.  

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
def clusterFieldId = "customfield_2354"
def clusterField = getFieldById(clusterFieldId)

def selectedValues = clusterField.getValue() as List<String>

if (selectedValues.contains("Windows") && (selectedValues.contains("Ubuntu") && selectedValues.contains("RHEL"))) {
UserMessageUtil.error("Invalid combination selected. Windows can not be selected with other values.")
clusterField.setFormValue(null)
}

return true

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.
February 18, 2024

Hi @TheAsh

I have reviewed your code, and your approach is incorrect.

You will need to configure a Server-Side Behaviour for the Multi-Select Field.

Below is a sample working code for your reference:-

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

@BaseScript FieldBehaviours behaviours

def os = getFieldById(fieldChanged)
def osValue = os.value as List<String>
os.clearError()

if (osValue.contains('Windows') && osValue.size() > 1) {
os.setError('When Windows is selected no other option is permitted')
}

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

If you observe the code above, I am not initialising the os field using the field name or field ID. Instead, I am using fieldChanged, i.e.:-

def os = getFieldById(fieldChanged)

When configuring a Server-Side Behaviour, the correct approach to initialise the field for which the Server-Side Behaviour is configured is using fieldChanged.

This ensures that the Behaviour will trigger when a change is made to that field. In this case, the Multi-User picker.

Below is a screenshot of the Server-Side Behaviour configuration for your reference:-

behaviour_config.png

Below are a couple of test screenshots for your reference:-

1. When I select any single option, either Ubuntu, Red Had or Windows only, no error message is returned as shown in the screenshots below:-

redhat_only.png

ubuntu_only.png

 

windows_only.png

2. If I select Ubuntu and Redhat together, as expected, no error message is returned as shown in the screenshot below:-

ubuntu_and_redhat.png

3. However, if I select Windows together with any other option, as expected the error message is returned as shown in the screenshots below:-

ubuntu_windows_error.png

windows_with_any_other.png

all_options_error.png

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

Thank you and Kind regards,

Ram 

Like TheAsh likes this
TheAsh February 19, 2024

Thank you @Ram Kumar Aravindakshan _Adaptavist_ , it works as expected!!

0 votes
Tansu Akdeniz
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 16, 2024

Hi @TheAsh 

Welcome to the community.

I would recommend you take a look at existing threads and vendor documentation. There are tutorials and sample cases.

If you already have a script, please share.

Add script to OS field. When user choose Windows, check other field values. If this is not allowed, use formField.setError(errorStr)

TheAsh February 16, 2024

Hi @Tansu Akdeniz Thank you for looking into my query. I have just updated and refined the conditions. Could you please review them and let me know if you need any additional information from my side?

Suggest an answer

Log in or Sign up to answer