Making custom fields invisible and mandatory based on component (Behaviours Plugin)

Kai Gottschalk
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.
October 27, 2015

Hi there,

I am building up a JIRA - support project for different Atlassian Tools (JIRA, Confluence, HipChat). Depending on the selected component (either JIRA, Confluence or HipChat), different customfields shall be either "visible & required" or "invisible & optional" in the create process.

 

Case 1 - Initial situation:

- Field "Business Unit" = Hidden

- Field "JIRA Project Concerned" = Hidden

 

Case 2 - Component "JIRA" being selected:

- Field "JIRA Project Concerned" = Visible and mandatory

 

Case 3 - Component "Confluence" being selected:

- Field "Business Unit" = Visible and mandatory

 

Case 4 - Component "HipChat" being selected:

_Shall be handled as case 1_ (both fields invisible and optional)

 

In my opinion this is a standard case for the behaviours plugin, but after several hours and a lot of code changes, I ended up with following code, which simply does not work for us:

import com.atlassian.jira.bc.project.component.ProjectComponent

 

FormField components = getFieldByName ("Component/s") // id for Component

FormField BusinessUnit = getFieldById ("customfield_10572") // id for "Business Unit"

FormField JiraProjectConcerned = getFieldById ("customfield_10100") // id for "Jira Project concerned""

 

String component =  components.getValue()

if (component == 17850) {

 JiraProjectConcerned.setHidden(false)

}

else {

 JiraProjectConcerned.setHidden(true)

}

Furthermore I optionally tried using following code in the initialiser script:

BusinessUnit.setHidden(true)

BusinessUnit.setRequired(false)

JiraProjectConcerned.setHidden(true)

JiraProjectConcerned.setRequired(false)

The result of the first snipplet is always that field "JIRA Project concerned" is hidden and will never show up, so the else-case seems to always true.

 

As mentioned it took me already a lot of time, being non code-savvy, so I'd be more than grateful to have any concrete hints into the right direction.

 

Thanks and best regards

Kai

PS: Already had an intense look into the documentation of the plugin at Adaptavist and in following questions here:

3 answers

1 accepted

1 vote
Answer accepted
Kai Gottschalk
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 19, 2016

@Jamie Echlin (Adaptavist) solved my question with this comment/link:

That particular example is covered here: https://scriptrunner.adaptavist.com/latest/jira/recipes/behaviours/select-list-other.html

So thanks a million once again, Jamie!

0 votes
Anusha Iyer January 9, 2019

Dear team,

In my Jira project's create screen, need to make LOCATION field mandatory only if Component/s Field's drop down value is "Cloud Test Lines" is selected..

Please help me with script or steps detail as I am a new bee to Jira.. It will be great help if you people direct me to get it solved immediately.

-

Anusha

0 votes
Thanos Batagiannis _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.
October 27, 2015

Hi Kai

You can try something like this. 

import com.atlassian.jira.bc.project.component.ProjectComponentImpl

def components = getFieldById("components") // id for Component
def JiraProjectConcerned = getFieldById ("customfield_10200") // id for "Jira Project concerned""

def component = components.getValue()
ProjectComponentImpl componentImpl = component[0]

if (componentImpl?.getName()?.equals('17850')) {
JiraProjectConcerned.setRequired(false)
}
else {
JiraProjectConcerned.setRequired(true)
}

Please let me know if this works for you

Regards

Kai Gottschalk
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.
October 27, 2015

Hi Thanos,

first: Thanks a million for pointing us into the correct direction!

With your script example above, I finally got it working 90% of our requirement, using this script:

import com.atlassian.jira.bc.project.component.ProjectComponentImpl

 

def components = getFieldById("components") // id for Component

def JiraProjectConcerned = getFieldById ("customfield_10100") // id for "Jira Project concerned"

def BusinessUnit = getFieldById ("customfield_10572") // id for "BusinessUnit"

 

def component =  components.getValue()

ProjectComponentImpl componentImpl = component[0]

 

if (componentImpl?.getName()?.equals('JIRA')) {

    JiraProjectConcerned.setRequired(true)

    BusinessUnit.setHidden(true)

}

else if (componentImpl?.getName()?.equals('Confluence')) {

    BusinessUnit.setRequired(true)

    JiraProjectConcerned.setHidden(true)

}

else if (componentImpl?.getName()?.equals('Hipchat')) {

    JiraProjectConcerned.setRequired(false)

    BusinessUnit.setRequired(false)

    JiraProjectConcerned.setHidden(true)

    BusinessUnit.setHidden(true)

}

The only missing thing here is that instead of removing fields ("JIRA Project concerned", "Business Unit") when a certain component is chosen, I wanted to remove the fields initially and then want to show them, once a certain component is chosen. My assumption was using the initialiser function for this, but it simply does not work. Any hint here for me?

Thanks a lot —

Cheers

Kai

JamieA
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.
October 28, 2015

If you want to show them when a component is chosen, you have to add the script to the component field, not initialiser.

Kai Gottschalk
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.
October 29, 2015

Hi Jamie, first of all thanks a lot for the best plugin available in Marketplace: Script Runner. We're using it for more than three years now, and - since three days - acquired a paid license ;) The above mentioned script is currently entirely attached in the "Component/s"-field and as stated I have following situation: as is: ------- 1.) Initially both fields ("Business Unit"/"JIRA Project concerned") are shown. 2.) Once a component (e.g. "JIRA") is entered the field not used ("Business Unit") DISAPPEARS. to be: -------- 1.) Initially both fields ("Business Unit"/"JIRA Project concerned") are NOT shown. 2.) Once a component (e.g. "JIRA") is entered the field used ("JIRA Project concerned") APPEARS. Is this doable? What I need to change in this script? Cheers Kai

Greg Hoggarth April 18, 2016

I'm also wanting to use Behaviours to control the visibility of other fields, depending on the value in a select box. I haven't yet tried to script it, but will be trying later today.

 

So if anyone has a solution or pointer for this problem, I'd be grateful. If I can get it working, I'll post my solution here as well.

JamieA
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 19, 2016
Greg Hoggarth April 19, 2016

Thanks!

sandeep4774 April 17, 2017

Hi Team,

I too have a similar kind of Issue, could you please help me?

 

I want to make Custom Field (Software) mandatory upon Component value as "software". I have tried followng code, but it didn't work:

 

import com.atlassian.jira.bc.project.component.ProjectComponentImpl

def components = getFieldById("components") // id for Component
def Software = getFieldById("Software") // id for "Software""

def component = components.getValue()
ProjectComponentImpl componentImpl = component[0]

if (componentImpl?.getName()?.equals('software')) {
Software.setRequired(true)
}
else {
Software.setRequired(false)
}

 

Suggest an answer

Log in or Sign up to answer