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

Scriptrunner Behaviours: Display options of a field based on a Prefix

Stephan Delhaye
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
December 4, 2023

 

 

Hi Community,

I need your help with the following scenario:

I have 2 custom fields ("Application" (15401) (Select List (Single Choice) and "Application Role(s)" (15400) (Select List (Multi Choice).

The field "Application Roles(s)" around 1000 options starting with a defined prefix ("SAP MDG*" or "SAP S/4HANA*").

Now I want to implement a scriprunner behaviour showing certain values in Application Role(s) (15400) and in the past (for fields with lower amount of options), we code like below:

{code}

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

// Get the fields
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def optionsManager = ComponentAccessor.getOptionsManager();
def Field2 = customFieldManager.getCustomFieldObject("customfield_15400")
def Field2_config = Field2.getRelevantConfig(getIssueContext());
def Field2_options = optionsManager.getOptions(Field2_config);
def Field2field = getFieldById("customfield_15400");
def Field1field = getFieldById("customfield_15401");
def Field1Value = Field1field.getValue()


if (Field1Value == null) {
Field2field.setHidden(true)
Field2field.setRequired(false)
}
else {
    Field2field.setHidden(false)
    Field2field.setRequired(true)
}

def AllowedValue = [ ]

if (Field1Value == "SAP Master Data Governance (MDG)"){
AllowedValue = ["SAP MDG_CONTROL EQUIPMENT - Category Approval","SAP MDG_CONVEYORS- Category Approval"]
} else if (Field1Value == "SAP S/4HANA"){
AllowedValue = ["SAP S/4HANA_ALM: Asset Operations Administrator (Asset) - SO 1010","SAP S/4HANA_ALM: Asset Operations Administrator (Asset) - SO 1030"]
} else if (Field1Value != "SAP Master Data Governance (MDG)" && Field1Value != "SAP S/4HANA") {
AllowedValue = ["N/A","Others"]
}
Field2field.setFieldOptions(Field2_options.findAll{it.value in (AllowedValue)})
{code}
As the field "Application Role(s)" has around 1000 options, I was wondering if and how the line with "AllowedValue" can be setup in such a way, that it searches for the options to display based on their Prefix (either "SAP MDG*" or "SAP S/4HANA*") ?
I'm looking forward for your response and help.
Kind Regards,
Stephan

 

 

1 comment

Comment

Log in or Sign up to comment
Stephan Delhaye
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
December 11, 2023

Just in case anyone has a similar scenario and is also looking for a solution:

 


Adaptavist Scriptrunner support provided the below KB Article:

Set Behaviour Multi and Single Select Options and Value - Adaptavist Library and the below sample code (as example for my scenario):

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

// Get the fieldsdef applicationField = getFieldById(fieldChanged)
def applicationValue = applicationField.getValue()
def applicationRoleField = getFieldById("customfield_15400");

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def optionsManager = ComponentAccessor.getOptionsManager();
def applicationRole = customFieldManager.getCustomFieldObject("customfield_15400")
def applicationRoleConfig = applicationRole.getRelevantConfig(getIssueContext())
def applicationRoleOptions = optionsManager.getOptions(applicationRoleConfig)

if (applicationValue == null) {
    applicationRoleField.setHidden(true)
    applicationRoleField.setRequired(false)
}
else {
    applicationRoleField.setHidden(false)
    applicationRoleField.setRequired(true)
}

def AllowedValue = [ ]

if (applicationValue == "SAP Master Data Governance (MDG)"){
    AllowedValue = applicationRoleOptions.findAll { 
        it.value.contains('SAP MDG')
    }
} else if (applicationValue == "SAP S/4HANA"){
    AllowedValue = applicationRoleOptions.findAll { 
        it.value.contains('SAP S/4HANA')
    }
} else if (applicationValue != "SAP Master Data Governance (MDG)" && applicationValue != "SAP S/4HANA") {
    AllowedValue = ["N/A","Others"]
} 

applicationRoleField.setFieldOptions(AllowedValue)

 

TAGS
AUG Leaders

Atlassian Community Events