error on setHidden

Ronen Erlich April 11, 2018

I am using Script Runner (Jira version 7.3), trying to hide a custom field text box (one line) unless "other" is being selected from another custom field drop down. 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.util.UserUtil;

def customFieldManager = ComponentAccessor.getCustomFieldManager()

// Get pointers to the custom field(s) required
def cf = customFieldManager.getCustomFieldObjectByName("Cost Center")
def cf1 = customFieldManager.getCustomFieldObjectByName("Cost Center (Other)")

// Get the Value of Select List A
def cfVal = getCustomFieldValue(cf).toString()

// Hide Select List B by default
cf1.setHidden(true)

// If option B is selected in Select List A show Select List B and make it required
if(cfVal == "Other (Fill in next Line)"){
cf1.setHidden(false) // Show Select List B
cf1.setRequired(true) // Make Select List B Required
}

but I am getting an error on all lines where I set.Hidden and set.Required:

Cannot find matching method com.atlassian.jira.issue.fields.CustomFields#setHidden (boolean)

I investigated and compared to other scripts online but could not find why I'm getting this error...

Any help is much appreciated!

2 answers

1 accepted

1 vote
Answer accepted
Roland Holban (Adaptavist)
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.
April 12, 2018

The getCustomFieldObjectByName function returns a custom field object. For your purposes, you need a form field which can be accessed with getFieldByName.

Here is code that should work.

// Get pointers to the custom field(s) required
def cf = getFieldByName("Cost Center")
def cf1 = getFieldByName("Cost Center (Other)")

// Hide Select List B by default
cf1.setHidden(true)

// If option B is selected in Select List A show Select List B and make it required
if (cf.value == "Other (Fill in next Line)") {
cf1.setHidden(false) // Show Select List B
cf1.setRequired(true) // Make Select List B Required
}
Ronen Erlich April 12, 2018

Thanks Roland!

This was my original code but it doesn't comply with Jira server 7.3.6 - this is why i changed it.

I did found that setHidden API is under a different class:

com.atlassian.jira.issue.fields.layout.field.FieldLayoutItemImpl.Builder;

but I am having issue to make it work as I need to re-define cf and cf1 to work with the layout...

Thank you,

Ronen

Ronen Erlich April 12, 2018

 Ideally I would like to be able to use: 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.user.util.UserUtil

// Get pointers to the custom field(s) required

def cf = getFieldByName("Select List A")

def cf1 = getFieldByName("Select List B")

 

// Get the Value of Select List A

def cfVal = cf.getValue().toString()

 

// Hide Select List B by default

cf1.setHidden(true)

 

// If option B is selected in Select List A show Select List B and make it required

if(cfVal == "B"){

    cf1.setHidden(false) // Show Select List B

    cf1.setRequired(true) // Make Select List B Required

}

 

In Jira 7.3 version. the above works with version 6.x but our team upgraded just about a year ago....and many of the scriptrunner scripts broke :-( 

I think adaptive should provide a library of conversions between Jira versions

Thanks,

Roland Holban (Adaptavist)
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.
April 12, 2018

The setHidden function is a ScriptRunner function and it comes from the Behaviour API. But regardless, you shouldn't need any imports for the script to work. I tested this on JIRA 7.6.2 and it worked.

Also, Im not sure what you mean by having to redefine cf and cf1 to work with your layout. If you're talking about the name of the fields you are trying to get a pointer to just change the arguments "Cost Center" and "Cost Center (Other)" to the name of your custom fields.

Roland Holban (Adaptavist)
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.
April 12, 2018

One more thing. Whenever trying to access the value you are typing:

def cfVal = cf.getValue().toString()

 This is unnecessary. The value can be accessed with:

cf.value  
Ronen Erlich April 12, 2018

Thanks Again,

I am still getting errorScreen Shot 2018-04-12 at 2.37.17 PM.pngmaybe my script runner version is too old... we have not upgraded it we are still on version 5.0.11...

Roland Holban (Adaptavist)
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.
April 12, 2018

Yeah it could be the version. I tested mine on 5.3.9 and it worked. 

You can try changing getFIeldByName to getFieldById(custom_field_id). You can find the id of a custom field by navigating to Custom Fields on JIRA, clicking the field you want, then looking at the end of the URL.

Ronen Erlich April 12, 2018

Thanks so much for your help Roland!

I tested previously getFieldById - this also generates the same error as in the screen shot above...

 

Akbar N October 29, 2018

Hi Ronen, Are you able resolve this issue? I'm still getting this issue in one of my script. 

 

Pls suggest how you have resolved it. 

Ronen Erlich November 1, 2018

Hi Akbar,

yes, i was able to make it work - i think the part that was missing was one of the libraries...

here is my code (using script runner behavior):

// Origin is the dropdown list and if you select option "Other Department or Team" than the "Department Name" field will be displayed, otherwise this field is hidden

Initializer script: 

//set the "Department Name" CF to hidden

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserUtil
import com.onresolve.jira.groovy.user.FormField

FormField dep_name = getFieldById("customfield_14394")
dep_name.setHidden(true)

 

Fields script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserUtil
import com.onresolve.jira.groovy.user.FormField

// Get pointers to the custom field(s) required
FormField origin= getFieldById("customfield_13390")
FormField dep_name = getFieldById("customfield_14394")

// Get the Value of Select List A (origin)
def origin_val = origin.getValue().toString()
// Hide "Other Department or Team" (this is needed as if user select and then select something else you would like to re-set the hidden field
dep_name.setHidden(true)

// If "Other Department or Team" is selected in "origin" show "Department Name" CF and make it required
if(origin_val == "Other Department or Team"){
dep_name.setHidden(false) // Show "Department Name"
dep_name.setRequired(true) // Make "Department Name" Required
}

enjoy!

Akbar N November 1, 2018

Thanks Ronen! I have got the same working through Behaviors yesterday. Here is the similar script.

def extCommSelectCf = getFieldById("customfield_18903") //get the field release date
def extCommmDateCf = getFieldById("customfield_18902") //get the field for External date

extCommmDateCf.setHidden(true);

if (extCommSelectCf.getValue() == "Yes"){
extCommmDateCf.setHidden(false);
extCommmDateCf.setRequired(true);

}

0 votes
Aleksandar Salevski September 28, 2019

Hi Guys,

 

Does anyone tested this on JIRA 8.1 and above?

cause on my server instance setHidden() method could not be found in any of the above imported library!?

 

Any luck on your side?

 

Regards,

Alex

DPE TEAM May 1, 2020

Hi Alex,

We run Jira 8.3.1 and ScriptRunner 5.6.15.1-p5 and I cannot get setRequired recognized. ScriptRunner_no_setRequired.png

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events