It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

error on setHidden

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

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
}

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

 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,

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.

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  

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...

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.

Thanks so much for your help Roland!

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

 

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. 

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!

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);

}

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

260 views 1 2
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you