how to traverse through a Select List (multiple choices) values using groovy script

Manjunatha K R January 9, 2017

Hi,

how to traverse through a Select List (multiple choices) values and count number of values updated in it using groovy script.

i have one requirement where I need to check the number of values updated as part of custom field i,e "Impacted sub-systems" of Select List (multiple choices). Please advice, how to do it?

 

I tried to use the below code/script based on the answers shared in our Atlassian community.

But unable to traverse for 2nd vlaue, etc using these methods - please suggest what is the right method to achieve the same.

  • Manju

 

1st Approach tried:
--------------------
//'Impacted sub-systems' is a multiple choice single select list custom field
def componentCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Impacted sub-systems")
def selectedComponents = issue.getCustomFieldValue(componentCF)
 
selectedComponents?.each { LazyLoadedOption it ->
}
 
2nd approach tried:
-------------------
def componentCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Impacted sub-systems")
 
Group group = issue.getCustomFieldValue(componentCF) as Group
if (group){group?.each { 
	def issueFactory = ComponentAccessor.getIssueFactory()
    def issueManager = ComponentAccessor.getIssueManager()
    def newIssue = issueFactory.cloneIssue(issue) 
}

 

 

5 answers

0 votes
Manjunatha K R January 11, 2017

Now using the 1st approach, I can traverse through the number of custom field values and able to create number of clones equivalent to number of custom field values updated in a JIRA issue now.

There was an issue with setCustomFieldValue of different data types and it's resolved now.

But I would like to create "Issue Links from parent JIRA issue(i,e from which I cloned) to Cloned issues in the below same loop.

Please advice how to create Issue links in both the directions means "is cloned by(Inward)" link to be established in the parent JIRA issue and "clones(Outward)" link to be established from all the cloned JIRA issues to a parent JIRA issue.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
 
Issue issue = issue
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
 
//'Impacted sub-systems' is a multiple select list custom field
def componentCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Impacted sub-systems")
def selectedComponents = issue.getCustomFieldValue(componentCF)
 
//'Subsystem' is a select list (single choice)
def subSystemCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Subsystem")
 
selectedComponents?.each { LazyLoadedOption it ->
    def issueFactory = ComponentAccessor.getIssueFactory()
    def issueManager = ComponentAccessor.getIssueManager()
    def newIssue = issueFactory.cloneIssue(issue)
 
    // because the sub-system is a select list (single choice) we should find the option with that value (if any)
    def optionToSet = ComponentAccessor.getOptionsManager().getOptions(subSystemCF.getRelevantConfig(issue)).find {option -> option.value == it.value}
newIssue.setCustomFieldValue(subSystemCF, optionToSet)
 
Map<String,Object> newIssueParams = ["issue":newIssue] as Map<String,Object>
    issueManager.createIssueObject(currentUser, newIssueParams)
}
0 votes
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.
January 11, 2017

You've said "through the updated multiple custom field values" again, without explaining it, and that's the main problem with your question.  Your "clone issues and inherit data" is now clear (which wasn't explained before), but you really need to explain the "updated".  Updated in what?

0 votes
manjunatha k r January 10, 2017

I would like to traverse through the updated multiple custom field values one by one, need to create those many clone issues and copy the custom field values one by one into another custom field of cloned issues respectively.

0 votes
Vasiliy Zverev
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.
January 9, 2017

Does it mean that on transition value of this custom field is changed and you need to find added/deleted options?

0 votes
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.
January 9, 2017

Your code doesn't really bear a lot of relationship to your question, apart from using iterators.

Could you explain your actual requirement though?  "how to traverse through a Select List (multiple choices) values and count number of values updated in it using groovy script." is not clear on the goal.  I can see you want to do something in a script, and that you want to work with values from a select list.  But not what you want to do.  "Number of values updated in what?"

Suggest an answer

Log in or Sign up to answer