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!
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Again,
I am still getting errormaybe my script runner version is too old... we have not upgraded it we are still on version 5.0.11...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks so much for your help Roland!
I tested previously getFieldById - this also generates the same error as in the screen shot above...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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.