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

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:

2 answers

1 accepted

This widget could not be displayed.

@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!

This widget could not be displayed.

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

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

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

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

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.

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
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

216 views 3 0
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you