Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to hide Select List Fields based on value of another Custom field - Server Side Script

bSte May 15, 2018

Help!  I am trying to hide and show certain fields in a single select drop down list if a different custom field value = retail.

 

Here is the code that isn't working:

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfigImpl
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField

import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours


def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def vertical = getFieldByName("vertical")
def verticalValue = vertical.getValue() as String
def typeOfEngagement = getFieldByName("type of engagement")
def typeOfEngagementCustomField = customFieldManager.getCustomFieldObjectByName("Type of Engagement")
def typeOfEngagementAp = customFieldManager.getCustomFieldObjectByName("Type of Engagement(Demographic Profiling)")
def fieldConfig = typeOfEngagementCustomField.getRelevantConfig(issueContext)

def isVerticalRetail = verticalValue == "Retail"


if (! isVerticalRetail){
typeOfEngagementAp.setHidden(true)
} else {

typeOfEngagementAp.setHidden(false)
}

2 answers

1 vote
Alexey Matveev
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.
May 15, 2018

Are there any errors in the atlassian-jira.log file?

bSte May 15, 2018

yes - the error is on the typeOfEngagementAp.setHidden(true) and typeOfEngagementAp.setHidden(false)  - its says: cannot find matching method

Alexey Matveev
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.
May 15, 2018

It should be like this

def vertical = getFieldByName("vertical")
log.error("vertical: " + vertical)
def verticalValue = vertical.getValue()
as String
log.error("verticalValue: " + verticalValue)
def typeOfEngagement = getFieldByName("type of engagement")
log.error("typeOfEngagement: " + typeOfEngagement)
def isVerticalRetail = verticalValue == "Retail"

if (! isVerticalRetail){
typeOfEngagement.setHidden(true)
} else {

typeOfEngagement.setHidden(false)
}

Also make sure that your field names are correct. getFIeldByName is case sensitive. Have a look in the log file to see the output

bSte May 15, 2018

Just to clarify, I am trying to hide selection options within the Type of Engagement field but not hide the entire field.  I am trying to hide it on the create screen after the vertical field is selected and based on the value entered into that field.

Alexey Matveev
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.
May 15, 2018

then you should use setFieldOptions instead of setHidden. You can find an example here:

https://scriptrunner.adaptavist.com/latest/jira/behaviours-api-quickref.html#_formfield_setfieldoptions_iterable

bSte May 15, 2018

Ok - I'm getting an error on formfield: the variable is undeclared.  How do you declare formfield - I can't find it in the docs.

 

import com.atlassian.jira.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfigImpl

import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def allowedOptions = null
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def vertical = getFieldByName("vertical")
def verticalValue = vertical.getValue()
def typeOfEngagement = getFieldByName("type of engagement")
def typeOfEngagementCustomField = customFieldManager.getCustomFieldObjectByName("Type of Engagement")
def fieldConfig = typeOfEngagementCustomField.getRelevantConfig(issueContext)
def customField = customFieldManager.getCustomFieldObject(getFieldChanged())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

if (verticalValue.toString() == "Retail"){
allowedOptions = formField.setFieldOptions(options.findAll {
it.value in ["Email", "Audience Workshop", "Audience Plan", "Media Plan", "RFP", "Proactive Audience Outreach", "Campaign Tracker", "Email"]
})
}else {
allowedOptions = formField.setFieldOptions(options.findAll {
it.value in ["Audience Planning", "Demographic Profiling", "Audience Matrix", "RFP", "Email"]
})
}
Alexey Matveev
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.
May 15, 2018

You have to attach the script to the  vertical field, which means that getFieldChanged() returns a reference to the vertical field. That is why your code should look like this:

def allowedOptions = null
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def vertical = getFieldByName("vertical")
def verticalValue = vertical.getValue()
def typeOfEngagement = getFieldByName("type of engagement")
def customField = customFieldManager.getCustomFieldObjectByName("Type of Engagement")
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

if (verticalValue.toString() == "Retail"){
allowedOptions = typeOfEngagement.setFieldOptions(options.findAll {
it.value in ["Email", "Audience Workshop", "Audience Plan", "Media Plan", "RFP", "Proactive Audience Outreach", "Campaign Tracker", "Email"]
})
}else {
allowedOptions = typeOfEngagement.setFieldOptions(options.findAll {
it.value in ["Audience Planning", "Demographic Profiling", "Audience Matrix", "RFP", "Email"]
})
}

Put logging there and make sure you provide valid names for your custom field.

bSte May 15, 2018

Thanks - I have updated the code and there are no errors but it doesn't actually work when I start to create a ticket.  It is suppose to change the dropdown options directly after the vertical custom field is selected. Currently, I have all the options in the dropdown list.

 

import com.atlassian.jira.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfigImpl

import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def allowedOptions = null
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def vertical = getFieldByName("vertical")
def verticalValue = vertical.getValue()
def typeOfEngagement = getFieldByName("type of engagement")
def typeOfEngagementCustomField = customFieldManager.getCustomFieldObjectByName("Type of Engagement")
def fieldConfig = typeOfEngagementCustomField.getRelevantConfig(issueContext)
def customField = customFieldManager.getCustomFieldObjectByName("Type of Engagement")
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

if (verticalValue.toString() == "Retail"){
allowedOptions = typeOfEngagement.setFieldOptions(options.findAll {
it.value in ["Email", "Audience Workshop", "Audience Plan", "Media Plan", "RFP", "Proactive Audience Outreach", "Campaign Tracker", "Email"]
})
}else {
allowedOptions = typeOfEngagement.setFieldOptions(options.findAll {
it.value in ["Audience Planning", "Demographic Profiling", "Audience Matrix", "RFP", "Email"]
})
}

re

Alexey Matveev
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.
May 15, 2018

I still strongly believe that you provided wrong names for fields. You should put logging in your script and have a look what is going wrong.

bSte May 15, 2018

Ok, so it works but not the way its suppose to, it just does the else statement.  How do I enter the logging into my script to see whats going on?

Alexey Matveev
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.
May 15, 2018

You add logging by log.error. That is the simplest one. You do not have to set logging levels. For example, if you want to see the verticalValue value then you can do like this:

def allowedOptions = null
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def vertical = getFieldByName("vertical")
def verticalValue = vertical.getValue()
def typeOfEngagement = getFieldByName("type of engagement")
def typeOfEngagementCustomField = customFieldManager.getCustomFieldObjectByName("Type of Engagement")
def fieldConfig = typeOfEngagementCustomField.getRelevantConfig(issueContext)
def customField = customFieldManager.getCustomFieldObjectByName("Type of Engagement")
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)
log.error("verticalValue: " + verticalValue)
if (verticalValue.toString() == "Retail"){
allowedOptions = typeOfEngagement.setFieldOptions(options.findAll {
it.value in ["Email", "Audience Workshop", "Audience Plan", "Media Plan", "RFP", "Proactive Audience Outreach", "Campaign Tracker", "Email"]
})
}else {
allowedOptions = typeOfEngagement.setFieldOptions(options.findAll {
it.value in ["Audience Planning", "Demographic Profiling", "Audience Matrix", "RFP", "Email"]
})
}

You can see the output in the atlassian-jira.log file. 

bSte May 16, 2018
import com.atlassian.jira.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfigImpl

import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def allowedOptions = null
def campaignRetailOptions = null
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def verticalCustomField = getFieldByName("Vertical")
def verticalValue = verticalCustomField.getValue()
def typeOfEngagement = getFieldByName("Type of Engagement")
def typeOfEngagementCustomField = customFieldManager.getCustomFieldObjectByName("Type of Engagement")
def campaignType = getFieldByName("Campaign Type")
def campaignTypeCustomField = customFieldManager.getCustomFieldObjectByName("Campaign Type")
def fieldConfig = typeOfEngagementCustomField.getRelevantConfig(issueContext)
def fieldConfigCRO = campaignTypeCustomField.getRelevantConfig(issueContext)
def customField = customFieldManager.getCustomFieldObjectByName("Type of Engagement")
def customFieldCRO = customFieldManager.getCustomFieldObjectByName("Campaign Type")
def config = customField.getRelevantConfig(getIssueContext())
def configCRO = customFieldCRO.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)
def optionsCRO = optionsManager.getOptions(configCRO)

if (verticalValue.toString() == "Retail"){
allowedOptions = typeOfEngagement.setFieldOptions(options.findAll {
it.value in ["Audience Planning", "Audience Workshop", "Campaign Tracker", "Email", "Media Plan", "Proactive Audience Outreach", "RFP"]});
campaignRetailOptions = campaignType.setFieldOptions(options.findAll {
it.value in ["Awareness", "New Store Opening", "Sales Event", "In Market", "Prospecting", "Retention"]})
}else {
allowedOptions = typeOfEngagement.setFieldOptions(options.findAll {
it.value in ["Audience Planning", "Audience Matrix", "Demographic Profiling", "Email", "RFP"]});
campaignRetailOptions = campaignType.setFieldOptions(options.findAll {
it.value in ["Awareness", "Launch", "Sales Event", "In Market"]
})
}
bSte May 16, 2018

Hi Alexey - the code is working now but I need to add additional fields in the if/else statement - do I do this with additional ifs even though they aren't dependent on each other or with an &&? Neither option works for me, both just create an empty drop down list. 

Alexey Matveev
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.
May 16, 2018

You can do it with the same ifs like you did in the code above.

0 votes
J Manas Kumar
Contributor
July 13, 2020

Hi @bSte i need your help, as i have a requirement same type like yours.

i have a field that has some options, i need to hide some options to user on based upon other customer field value.

how ot show specific 2 or 3 options if field1 has some value?

 

 

Regards

Manas

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events