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

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 vote

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

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

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 vote

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?

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

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,296 views 14 20
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot