Forums

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

Cascading Select "Set Field Value to constant or Groovy expression Function"

Cole
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.
September 23, 2016

Trying to get this post function to work using cascading select, but I am not sure how. I want to set the cascading select options to be a certain value.

1 answer

1 accepted

1 vote
Answer accepted
David _old account_
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.
September 23, 2016

The "easiest" is to pass the Option ID as a string. The Option ID is visible at the end of the "Edit" link on the Edit Options screen of the cascading custom field.

If the child option's textual value is unique in the entire cascading field's values, you can also pass that value.

We will also make it easier to set cascading field values in an upcoming version of JMWE.

David

Cole
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.
September 23, 2016

Can you give me an example of the expression i would use? I normally just set the field and do a raw value of what i want it to be.

David _old account_
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.
September 23, 2016

If it's a constant value you want to set, you go to the configuration of the cascading custom field, navigate to the appropriate parent value, and then click on the Edit link for the child value:

Capture d’écran 2016-09-23 à 15.09.42.png

Capture d’écran 2016-09-23 à 15.12.55.png

Then you look at the URL of the page. At the end, you should see something like: &selectedValue=10102. 10102 would then be the value you type in the Value field of the post-function.

Cole
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.
September 23, 2016

Then it would just literally tell the field to set to that numeric value, right? It did not work when I tried it.

David _old account_
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.
September 23, 2016

It would set the field to the value whose ID is that numeric value, yes. I'm surprised it didn't work, I'll need to test it further.

You didn't select "Groovy", did you?

Cole
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.
September 26, 2016

I did not select Groovy. I did raw value. I'm not sure what i am doing wrong here.

David _old account_
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.
September 26, 2016

Actually, I just tested it and it is indeed a regression related to JIRA 7. 

However, you can still set a value on a cascading select field, but the code is rather complicated:

import com.atlassian.jira.component.ComponentAccessor;
def optionsManager = ComponentAccessor.getOptionsManager();
def parentOptionObj = optionsManager.findByOptionId(10000);
def childOptionObj = optionsManager.findByOptionId(10002);
Map<String,Object> newValues = new HashMap<String, String>();
newValues.put(null, parentOptionObj);
newValues.put("1", childOptionObj);
return newValues;

where 10000 and 10002 are the IDs of the parent and child options.

Cole
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.
September 27, 2016

So would I just put all of that in the box and change it to Groovy instead of Raw Value?

David _old account_
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.
September 27, 2016

Exactly.

Cole
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.
September 27, 2016

Awesome! So for times when there is no child, I guess it would be the default "None" option, how can I tell it to only set the parent?

Cole
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.
September 27, 2016

I changed mine to remove the child option obj and change the newValues, but it is not working currently.

 

import com.atlassian.jira.component.ComponentAccessor;
def optionsManager = ComponentAccessor.getOptionsManager();
def parentOptionObj = optionsManager.findByOptionId(19248);
Map<String,Object> newValues = new HashMap<String, String>();
newValues.put("Help Desk", parentOptionObj);
newValues.put(null, childOptionObj);
return newValues;

 

Even when I leave that info in it does not seem to work:

 

import com.atlassian.jira.component.ComponentAccessor;
def optionsManager = ComponentAccessor.getOptionsManager();
def parentOptionObj = optionsManager.findByOptionId(18683);
def childOptionObj = optionsManager.findByOptionId(19247);
Map<String,Object> newValues = new HashMap<String, String>();
newValues.put("ERP", parentOptionObj);
newValues.put("EDI", childOptionObj);
return newValues;

I actually get an error with it when I try to trigger it with the transition button.

 

"Error occurred while creating issue. This could be due to a plugin being incompatible with this version of JIRA. For more details please consult the logs, and see: http://confluence.atlassian.com/x/3McB java.util.HashMap cannot be cast to java.util.Collection

It seems that you have tried to perform an illegal workflow operation.

If you think this message is wrong, please contact your JIRA administrators."

David _old account_
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.
September 27, 2016

The first parameter (the "key") of the put() method is always null for the parent value, and "1" for the child value. Never change them.

To set just the parent value, I believe you just omit the child ("1") value:

import com.atlassian.jira.component.ComponentAccessor;
def optionsManager = ComponentAccessor.getOptionsManager();
def parentOptionObj = optionsManager.findByOptionId(19248);
Map<String,Object> newValues = new HashMap<String, String>();
newValues.put(null, parentOptionObj);
return newValues;
Cole
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.
September 27, 2016

Oh wow, I see now. That worked perfectly! Thank you very much!

Cole
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.
September 27, 2016

One more quick thing and I should be all set. What if I want it to be used in an if statement? So it would set the cascading field to one thing based on a certain issue type, but if it is not that issue type it would not.

Here is what I have tried so far:

import com.atlassian.jira.component.ComponentAccessor;

if (issue.issueTypeObject.name == "Account Administration"){
def optionsManager = ComponentAccessor.getOptionsManager();
def parentOptionObj = optionsManager.findByOptionId(19239);
def childOptionObj = optionsManager.findByOptionId();
Map<String,Object> newValues = new HashMap<String, String>();
newValues.put(null, parentOptionObj);
newValues.put("1", childOptionObj);
return newValues;
}
else {
def optionsManager = ComponentAccessor.getOptionsManager();
def parentOptionObj = optionsManager.findByOptionId(19248);
def childOptionObj = optionsManager.findByOptionId();
Map<String,Object> newValues = new HashMap<String, String>();
newValues.put(null, parentOptionObj);
newValues.put("1", childOptionObj);
return newValues;
}
David _old account_
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.
September 27, 2016

Your code is moving in the right direction except that "def childOptionObj = optionsManager.findByOptionId();" won't work because you need to pass an Option ID. If you don't need a child option, just don't create that variable and don't put it into the Map.

When you code doesn't work, always look into your JIRA logs (atlassian-jira.log) for ERRORs.

Cole
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.
September 28, 2016

Well, even when I remove that it does not work. I have checked the log you mentioned, but all it says is that there was an error dealing with a Groovy script, not what is actually wrong.

 

My current code:

import com.atlassian.jira.component.ComponentAccessor;

if (issue.issueTypeObject.name == "Account Administration"){
def optionsManager = ComponentAccessor.getOptionsManager();
def parentOptionObj = optionsManager.findByOptionId(19239);
Map<String,Object> newValues = new HashMap<String, String>();
newValues.put(null, parentOptionObj);
return newValues;
}
else {
def optionsManager = ComponentAccessor.getOptionsManager();
def parentOptionObj = optionsManager.findByOptionId(19248);
Map<String,Object> newValues = new HashMap<String, String>();
newValues.put(null, parentOptionObj);
return newValues;
}
David _old account_
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.
September 28, 2016

Actually, the ERROR should tell you what's wrong. But be careful, it's a multi-line error description so "grep" won't work.

In your particular case, I think the error (mine, actually) is on the following line (repeated twice):

Map<String,Object> newValues = new HashMap<String, String>();

It should be 

Map<String,Object> newValues = new HashMap<String, Object>();

or even simpler:

def newValues = new HashMap();
Cole
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 17, 2016

Hey David, 

I know this question is old, but I thought I might post here. I am having the same issue.

My code:

import com.atlassian.jira.component.ComponentAccessor;
 
if (issue.get("customfield_18871") != "1" || issue.get("customfield_18871") != "2" || issue.get("customfield_18871") != "3"){
def optionsManager = ComponentAccessor.getOptionsManager();
def parentOptionObj = optionsManager.findByOptionId(19472);
def childOptionObj = optionsManager.findByOptionId(19488);
Map<String,Object> newValues = new HashMap<String, String>();
newValues.put(null, parentOptionObj);
newValues.put("1", childOptionObj);
return newValues;
}

It just makes this field always have this value. Even if 1,2, or 3 is selected.

David _old account_
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 17, 2016

You want an "and" ("&&"), not an "or" (||) between the conditions.

David

Cole
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 18, 2016

You're right... My logic was just messed up in my head. You are the best! Thanks so much!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events