Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Need to display only limited options for a second custom field option.

Ashok Barik
Contributor
March 13, 2022

Hello Community, 

Could you please assist me to display only limited options for a second custom field option and also let me know which script runner option I should use? Is it the option Behaviours because I am very new to the groovy script.

 
For example,

I have two fields: Field1 and Field2.

Field1 contains three dropdown menus: A, B, and C.
Field2 has the following dropdown options: E, F, G, H, and I.


Now all I need are options for field2 to display E, F, and I If, A is selected in Field1. and the same, if B is selected, options F and G should be displayed, and if C is selected, options E and I should be displayed.

2 answers

1 accepted

0 votes
Answer accepted
Nic Brough -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.
March 13, 2022

Your description of what you're trying to achieve is screaming "cascading select list" to me.  Jira has these off-the-shelf, no need to mess with Behaviours or any other complexities.

If cascading select lists are not right for you, could you explain why?

Ashok Barik
Contributor
March 14, 2022

I have already defined a filed script of those custom field values.  It's okay with me to use cascading field but I need the correct script. Could you please help me with my below script to convert from custom filed to cascading? 

-------------------------------------------------------------

Below are two fields that I am changing to Cascading. 

customfield_18840 and customfield_18841

---------------------------------------------------------------

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.search.SearchProvider

import com.atlassian.jira.web.bean.PagerFilter

import com.atlassian.jira.issue.IssueManager;

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.link.IssueLinkManager

import com.atlassian.jira.user.ApplicationUser

 

def issueManager = ComponentAccessor.getIssueManager()

def linkManager = ComponentAccessor.getIssueLinkManager()

def cfm = ComponentAccessor.getCustomFieldManager()

 

Issue issueKey = issue

def id=issueKey.getId()

 

def customFieldManager = ComponentAccessor.getCustomFieldManager();

 

def cf = customFieldManager.getCustomFieldObject("customfield_18840") //field1 ID - Category Field

def cff = customFieldManager.getCustomFieldObject("customfield_18841") // field2 ID - Size field

def field3 = customFieldManager.getCustomFieldObject("customfield_18844") // field3 ID - Actual hour work

 

def n = issue.getCustomFieldValue(cf).toString()

def b = issue.getCustomFieldValue(cff).toString()

def c = Integer.parseInt((issue.getCustomFieldValue(field3) ?: 0).toString().replaceAll(~/[.].*/, ""))

def d = 0;

if(n=="CONCEPT" && b =="M"){ d = 48 }

if(n=="CONCEPT" && b =="M"){ d = 48 }

if(n=="POC" && b =="S"){ d = 30 }

if(n=="POC" && b =="M"){ d = 60 }

if(n=="SYSSPEC" && b =="S"){ d = 29 }

if(n=="SYSSPEC" && b =="M"){ d = 58 }

 

Integer remaining = d - c

return remaining

 

------------------------------------------------

 

Thank you for your support!

Nic Brough -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.
March 14, 2022

I don't understand.  Why are you not just using a cascading select list?

Ashok Barik
Contributor
March 14, 2022

Hello @Nic Brough -Adaptavist- 

I have already configured the cascading select list and it is now functional. 

but I need to get a calculated automation result from both cascade values and one more field value. 

Example:

We have predefined the hour of matching value from fields 1 and 2.
For example, if I choose A from field1 and B from field2, the total hour is 60. (There are multiple total hours values according to the field1 and field2)

In my case, I want the remaining hour in the result if someone mentions his work hour in the filed3 then

Example:

Field1=A
Field2=B
(AB total hours = 60)
Field3 (My working hour) = 4
Remaining Hour I want = 60 - 4 = 56hr

 

I was using the above script before configuring cascading select list.  Could you please change the script for me? 

Radek Dostál
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.
March 14, 2022

This really sounds like Time Tracking now.

The only difference with the value you get from a Cascading field is that instead of a single value you would get before (aka single-select list returns single option), you now get a map instead. E.g. [null:parentValue, 1:childValue].

 

So you're currently getting the value like this

def n = issue.getCustomFieldValue(cf).toString()

So instead, don't convert to String, parse the map first, something like this

def cascadingValue = issue.getCustomFieldValue(cf)
def parent = cascadingValue?.get(null).toString()
def child = cascadingValue?.get("1").toString()

 

You can do 'log.warn("Current Cascading Value: " + cascadingValue) so you can see what the map looks like, but it's either: null, [null:parent], or [null:parent, 1:child]

 

In any case I am also somewhat bemused how or why this is setup this way, because this kind of hour tracking is.. well it's not exactly a robust setup. Time Tracking is there for that.

Ashok Barik
Contributor
March 14, 2022

Kindly review my script and make it correct as I'm getting error on line no -29

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.MutableIssue
import com.opensymphony.workflow.InvalidInputException

//def cf = issue.getCustomFieldValue(cff).toString()


def issueManager = ComponentAccessor.getIssueManager()
def linkManager = ComponentAccessor.getIssueLinkManager()
def cfm = ComponentAccessor.getCustomFieldManager()

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def field3 = customFieldManager.getCustomFieldObject("customfield_18844") // field3 ID - Actual hour work
//def cf = customFieldManager.getCustomFieldObject("customfield_18840") //field1 ID - Category Field
//def cff = customFieldManager.getCustomFieldObject("customfield_18841") // field2 ID - Size field
Issue issueKey = issue
def id=issueKey.getId()

final myField = "Category"
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("myField")
//def issue = issue as MutableIssue
def cfValue = issue.getCustomFieldValue(cf) as HashMap
//def cfValue = issue.getCustomFieldValue(cf) as HashMap

def n = cfValue?.get(0).toString()
def b = cfValue?.get(1).toString()

if (!b) {
throw new InvalidInputException("Child option is Null")
}

//def n = issue.getCustomFieldValue(parentOption).toString()
//def b = issue.getCustomFieldValue(childOption).toString()
def c = Integer.parseInt((issue.getCustomFieldValue(field3) ?: 0).toString().replaceAll(~/[.].*/, ""))
def d = 0;
if(n=="CONCEPT" && b =="M"){ d = 48 }
if(n=="CONCEPT" && b =="M"){ d = 48 }
if(n=="POC" && b =="S"){ d = 30 }
if(n=="POC" && b =="M"){ d = 60 }
if(n=="POC" && b =="L"){ d = 120 }
if(n=="SYSSPEC" && b =="S"){ d = 29 }
if(n=="SYSSPEC" && b =="M"){ d = 58 }


Integer remaining = d - c
return remaining

Radek Dostál
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.
March 14, 2022

0 is not null, and 1 is not "1", see my snippet, it contains the correct get() syntax.

2 votes
Radek Dostál
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.
March 13, 2022

Why not use a cascading field? It does exactly what you describe - parent & child values.

Ashok Barik
Contributor
March 14, 2022

I have already defined a filed script of those custom field values.  It's okay with me to use cascading field but I need the correct script. Could you please help me with my below script to convert from custom filed to cascading? 

-------------------------------------------------------------

Below are two fields that I am changing to Cascading. 

customfield_18840 and customfield_18841

---------------------------------------------------------------

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.search.SearchProvider

import com.atlassian.jira.web.bean.PagerFilter

import com.atlassian.jira.issue.IssueManager;

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.link.IssueLinkManager

import com.atlassian.jira.user.ApplicationUser

 

def issueManager = ComponentAccessor.getIssueManager()

def linkManager = ComponentAccessor.getIssueLinkManager()

def cfm = ComponentAccessor.getCustomFieldManager()

 

Issue issueKey = issue

def id=issueKey.getId()

 

def customFieldManager = ComponentAccessor.getCustomFieldManager();

 

def cf = customFieldManager.getCustomFieldObject("customfield_18840") //field1 ID - Category Field

def cff = customFieldManager.getCustomFieldObject("customfield_18841") // field2 ID - Size field

def field3 = customFieldManager.getCustomFieldObject("customfield_18844") // field3 ID - Actual hour work

 

def n = issue.getCustomFieldValue(cf).toString()

def b = issue.getCustomFieldValue(cff).toString()

def c = Integer.parseInt((issue.getCustomFieldValue(field3) ?: 0).toString().replaceAll(~/[.].*/, ""))

def d = 0;

if(n=="CONCEPT" && b =="M"){ d = 48 }

if(n=="CONCEPT" && b =="M"){ d = 48 }

if(n=="POC" && b =="S"){ d = 30 }

if(n=="POC" && b =="M"){ d = 60 }

if(n=="SYSSPEC" && b =="S"){ d = 29 }

if(n=="SYSSPEC" && b =="M"){ d = 58 }

 

Integer remaining = d - c

return remaining

 

------------------------------------------------

 

Thanks for support!

Ashok Barik
Contributor
March 24, 2022

Hi @Radek Dostál 

Please help me here,  as I am getting the negative value in the remaining field if the category and size field is blank.  Could you please help me to put a condition here, if the category field is blank then the remaining time script result should not be shown in the issue? 

I have used below but it not working . 

if (cf == null) {
return
}

Daniel Garcia May 10, 2022

A map with keys null and 1 is a very strange data structure. Best not to think why it was done like that.

Suggest an answer

Log in or Sign up to answer