Hiding field based on component selection Edited

Hi Team,

I want to hide some fields based on component/s selection.

If I Select components "a" 

Component.PNGthen in Cost Tab

Cost.PNG

Salesforce Cost and ESB Cost field should hide.  

below are my code which I coded in Behaviours

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.bc.project.component.ProjectComponentImpl;
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItemImpl.Builder;


def Cerilion_Cost = getFieldByName("Cerilion Cost");
def Salesforce_Cost = getFieldByName("Salesforce Cost");
def ESB_Cost = getFieldByName("ESB Cost");

def component =getFieldById("components");

if(component.getValue()=='a'){
Cerilion_Cost.setRequired(true);
Salesforce_Cost.setHidden(true);
ESB_Cost.setHidden(true);

}

 

The above code is not working , can anyone tell me where am doing mistake.

 

Thanks,

Parthiban

2 comments

Hey Parthiban!

I think you may just not be going down far enough with the component/s value. The component returns a map, and so in your condition your just checking if that returned map equals a string. You need to get the name attribute from the component like this:

def comp = getFieldById(getFieldChanged())
if(comp.value)
{
log.warn("Component: "+comp.value.name)
}

Try that out and see if it works for you! :D

Aidan

Hi Aidan,

 

Thanks for your reply, I got the selection and it works. Below are my code

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.bc.project.component.ProjectComponentImpl;
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItemImpl.Builder;
import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.bc.project.component.ProjectComponent;
import com.onresolve.jira.groovy.user.FieldBehaviours;
import groovy.transform.BaseScript;
import org.apache.log4j.Logger;
import org.apache.log4j.Level;
import com.onresolve.jira.groovy.user.FormField;
import com.atlassian.jira.bc.project.component.ProjectComponentImpl

@BaseScript FieldBehaviours fieldBehaviours
def log = Logger.getLogger("com.acme.CreateSubtask");
log.setLevel(Level.DEBUG);

def CCost = getFieldById("customfield_10302");
def SCost = getFieldById("customfield_10303");
def ECost = getFieldById("customfield_10304");

def components =getFieldById("components");
def comp= components.getValue()*.getName();


if (comp.toString().contains('a')){
CCost.setHidden(false);
SCost.setHidden(true);
ECost.setHidden(true);
}
else if(comp.toString().contains('b')){
CCost.setHidden(true);
SCost.setHidden(false);
ECost.setHidden(true);
}
else if (comp.toString().contains('c')){
CCost.setHidden(true);
SCost.setHidden(true);
ECost.setHidden(false);
}

 

But the problem here is , my client having 53 components as per my above code I need to write more than 150 If and else if condition.

 

Do you have any smarter way to check the above code for 53 components.

Requirement : My client having total 53 component , they will be select one or many components at a time . Based on the selection I need to show the cost field.

 

Kindly share your thoughts.

 

Thanks,

Parthiban

Comment

Log in or Sign up to comment
Community showcase
Published 6 hours ago in Jira Ops

Jira Ops Early Access Program Update #5: Three new ways to get a live Jira Ops demo!

Welcome to your weekly Jira Ops Early Access program update, where we’re sharing news and updates on Jira Ops progress as we work toward our 1.0 release. If you ever want to drop us feedback or ideas...

19 views 0 1
Read article

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