Post function script get value of simple selection list

Arthur SALMON October 1, 2019

Hi community !

 

I am trying to set up a post-function script in Jira workflow using scriptRunner.

I made it work for multiple selection list but it is not working for simple selection list...

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.comments.CommentManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.util.ImportUtils;
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.user.UserUtils

def String m_assignee = 'Unassigned';
def cfIssueType = issue.issueType.name;
def cfLoc = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Supplier Location");
Map cfLocation = issue.getCustomFieldValue(cfLoc) as Map

String first = cfLocation.get(null);
switch(first){
case 'North America':
m_assignee = 'iron.man';
break;
case 'Other':
m_assignee = 'spider.man';
break;
default :
m_assignee = 'super.man';
break;
}

issue.setAssigneeId(m_assignee);

 

I will have to combine this code with some other but first, I'm trying to make this block worked.

Can anyone give support on that please?

1 answer

1 accepted

2 votes
Answer accepted
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 1, 2019

You are very close - most people with this question don't realise the value from a select list is an array of elements.  (I'm not sure it's a map, I've just used arrays and collections in the past, so I'm not qualified to comment if that could be an extra problem)

The contents of the field is a list of one, but for a select list, the elements are "options", not strings, so your "first" variable will not contain what you expect. I think you need to treat it as an option.

The code here is what I tend to "borrow" nowadays - https://library.adaptavist.com/entity/update-priority-based-on-a-custom-field

Arthur SALMON October 1, 2019

Ok, I am not a hundred percent sure about what is the real difference but at least, I made it work thanks to you ! What I don't get is why is it working like this for "Single Select List" and not for "Cascading Select List"...?

 

Here is the code :

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.comments.CommentManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.util.ImportUtils;
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.user.UserUtils
import com.atlassian.jira.config.PriorityManager
//Line added 1
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption

def String m_assignee = 'Unassigned';
def cfIssueType = issue.issueType.name;
def cfLoc = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Supplier Location");
//Line added 2 (just added "as LazyLoadedOption")
def first = issue.getCustomFieldValue(cfLoc) as LazyLoadedOption;

switch(first){
case "North America":
m_assignee = 'iron.man';
break
case "Other":
m_assignee = 'spider.man';
break
default:
m_assignee = 'super.man';
break
}
issue.setAssigneeId(m_assignee);

 

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 2, 2019

It's about different java objects being stored, and objects in java can have very different properties and behaviours.  

For example, a text field holds a string of characters which works as you would expect.  Numeric fields hold simple numbers.  These objects are significantly different because you can't do things like multiply strings together, or put letters (other than scientific notations) into numbers.

The objects that Jira has as "options" are more complex again.  They are made up of a unique id, a display name, a type, flags to say if they are enabled or not, methods to read and update these things, and data to say what their parent option is if they are an option in the second part of a cascading-select list.

A cascading select is a single value still, but the option you get back is more complex, and you need to look at the parent as well as child option.

https://docs.atlassian.com/software/jira/docs/api/8.4.0/index.html?com/atlassian/jira/issue/customfields/option/Options.html covers the basics.

Like # people like this

Suggest an answer

Log in or Sign up to answer