Forums

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

Groovy script required to select values from a multi select list which are highlighted

Peter June 11, 2020

So i have a script that will create subtasks from values populated in a multiselect custom field, but it uses all the values in the custom field and not just the highlighted ones. Is there a way i can edit this script to use only the select values of the custom field??


import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.component.ComponentAccessor

// Does not run if the ticket is a subtask
if ( issue.getIssueType().isSubTask() ) return 0

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def constantManager = ComponentAccessor.getConstantsManager()
def issueManager = ComponentAccessor.getIssueManager()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def subTaskTypeId = constantManager.getAllIssueTypeObjects().find{ it.getName() == "Sub-Task" }.id
List<String> summariesList = []

// Get the values from the custom field 'Carrier' - Should probably change this to DB value of Carrier..
def buildFromField = customFieldManager.getCustomFieldObjects(issue)find {it.name == "Carrier"}
def optionsFieldConfig = buildFromField.getRelevantConfig(issue)
def optionsFound = optionsManager.getOptions (optionsFieldConfig)


// Compile the summary name of the subtask to be created
optionsFound.each { optionName ->
summariesList.add( optionName.getValue() + ": " + issue.getSummary())
}

// Create the subtasks
summariesList?.each { subTaskSummary ->
MutableIssue newSubTask = issueFactory.getIssue()

newSubTask.setSummary(subTaskSummary)
newSubTask.setParentObject(issue)
newSubTask.setProjectObject(issue.getProjectObject())
newSubTask.setIssueTypeId(subTaskTypeId)

Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
issueManager.createIssueObject(currentUser, newIssueParams)
subTaskManager.createSubTaskIssueLink(issue, newSubTask, currentUser)
}

1 answer

0 votes
Nic Brough -Adaptavist-
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.
June 11, 2020

This line is the problem:

def optionsFound = optionsManager.getOptions (optionsFieldConfig)

It's going off to the field definition to get the list of possible options.  You really need to be reading the data from the issue, not the custom field definition.

That means something like

def customFieldValue = issue.getCustomFieldValue(customField)

For a multi-select list, that will return an array of values you can iterate over

Peter June 15, 2020
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.CustomField;

// Does not run if the ticket is a subtask
if ( issue.getIssueType().isSubTask() ) return 0


def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def constantManager = ComponentAccessor.getConstantsManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def subTaskTypeId = constantManager.getAllIssueTypeObjects().find{ it.getName() == "Sub-Task" }.id
List<String> summariesList = []

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def carrier = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Carrier")
def carrierValue = issues.getCustomFieldValue(carrier)


// Compile the summary name of the subtask to be created
optionsFound.each { optionName ->
summariesList.add( optionName.getValue() + ": " + issue.getSummary())
}

// Create the subtasks
summariesList?.each { subTaskSummary ->
MutableIssue newSubTask = issueFactory.getIssue()

newSubTask.setSummary(subTaskSummary)
newSubTask.setParentObject(issue)
newSubTask.setProjectObject(issue.getProjectObject())
newSubTask.setIssueTypeId(subTaskTypeId)

Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
issueManager.createIssueObject(currentUser, newIssueParams)
subTaskManager.createSubTaskIssueLink(issue, newSubTask, currentUser)
}

 

Totally stuck..  keep getting this error..

 

Message:
groovy.lang.MissingPropertyException: No such property: issues for class: script1592223512190699815626
Stack:
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:65)
org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:51)
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:309)
script1592223512190699815626.run(script1592223512190699815626.groovy:25)

Suggest an answer

Log in or Sign up to answer