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

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
Accepted answer

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

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.

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.

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

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?

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

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.

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

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?

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."

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;

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

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;
}

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.

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;
}

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();

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.

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

David

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
Community showcase
Published Oct 31, 2018 in Marketplace Apps

Marketplace Spotlight: Zephyr

Hello Atlassian Community! Each month, we run a series of Spotlights to highlight Marketplace vendors and apps that our team thinks this Community would find valuable. In last month's Spotlig...

316 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