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:
Hi @TheAsh ,
You can achieve this by creating a simple scripted validator in the workflow.
Open the Respected Workflow:
Edit the Workflow:
Select the Desired Transition:
Click Validator:
Add Validator:
Select "Simple Scripted Validator" [ScriptRunner]:
Configure Condition Field:
!('Windows' in cfValues['OS']*.value && ('Ubuntu' in cfValues['OS']*.value || 'RHEL' in cfValues['OS']*.value))
Enter Validation Message:
Select Corresponding 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."
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @TheAsh
Could you please share the updated code so I can review it?
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:-
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:-
2. If I select Ubuntu and Redhat together, as expected, no error message is returned as shown in the screenshot below:-
3. However, if I select Windows together with any other option, as expected the error message is returned as shown in the screenshots below:-
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.