How to (a) add multiple Components and (b) set a Cascading Select field using post function script

Wayne Cranford November 14, 2018

Environment - Adaptavist ScriptRunner v5.3.5 with JIRA v 7.4.4

Using a Post Function Script to clone a number of product-specific issues so the individual teams can work each individually.  I have everything working except the ability to add multiple Component/s and populate a Cascading Select field.

Multiple Components: The following does us to set a single component, but need to add up to 5 different Components on several of the cloned issues. I searched online and could not find any examples on how to accomplish.

def projectComponentManager10 = ComponentAccessor.getProjectComponentManager()

def newcomponent10= projectComponentManager10.findByComponentName(project10.getId(), "FSU")

 issue.setComponent([newcomponent10])

Populate a Cascading Select - I tried using the example found in the behaviours section, (https://scriptrunner.adaptavist.com/4.3.7/jira/recipes/behaviours/setting-default-fields.html#_setting_cascading_select_values)  but no luck. I hacked around for 3 days and unable to find anything that works.

Any and all suggestions will be greatly appreciated.

Here is an example of one of the scripts in one of the Additional Issue Actions:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.watchers.WatcherManager

def project2 = issue.getProjectObject()
def componentManager = ComponentManager.instance
def optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)

def userUtil = ComponentAccessor.getUserUtil()

MutableIssue issue = issue

def projectComponentManager2 = ComponentAccessor.getProjectComponentManager()
def newcomponent2= projectComponentManager2.findByComponentName(project2.getId(), "AOE")
issue.setComponent([newcomponent2])

def customFieldManager2 = ComponentAccessor.getCustomFieldManager()
def cf2 = customFieldManager2.getCustomFieldObjectByName("SV Lead")
ApplicationUser user2 = userUtil.getUserByKey("ploveman")
ApplicationUser user22 = userUtil.getUserByKey("")
List<ApplicationUser> newApprovers2 = []
newApprovers2.add(user2)
newApprovers2.add(user22)

def customFieldManager21 = ComponentAccessor.getCustomFieldManager()
def cf21 = customFieldManager21.getCustomFieldObjectByName("SV Management")
ApplicationUser user21 = userUtil.getUserByKey("pkerr")
ApplicationUser user212 = userUtil.getUserByKey("")
List<ApplicationUser> newApprovers21 = []
newApprovers21.add(user21)
newApprovers21.add(user212)

String SUMMARY_VALUE2 = issue.summary
String newSummary2 = "Security Vulnerability (AOE) - "+SUMMARY_VALUE2

issue.assigneeId = "aoe_triage"
issue.summary = newSummary2
issue.setCustomFieldValue(cf2, newApprovers2)
issue.setCustomFieldValue(cf21, newApprovers21)

checkLink = {link -> false};

 

 

1 answer

Suggest an answer

Log in or Sign up to answer
1 vote
Roland Holban (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.
November 19, 2018

Here is an example of setting a cascading select list:

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager
def issueManager = ComponentAccessor.issueManager

def issue = issueManager.getIssueObject("issue_key")

def customField = customFieldManager.getCustomFieldObjectsByName("select_list_name")[0]
def fieldConfig = customField.getRelevantConfig(issue)

def options = optionsManager.getOptions(fieldConfig)
def parentOption = options.find { it.value == "parent_option_name" }
def childOption = parentOption?.childOptions?.find { it.value == "child_option_name" }

def newValues = [:]

newValues.put(null, parentOption)
newValues.put("1", childOption)

issue.setCustomFieldValue(customField, newValues)
Wayne Cranford November 19, 2018

Roland -thank you, I was able to implement from your example.   I took out the "def issue =" as I received the following error :

2018-11-19 14:15:47,930 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: XSS-973, actionId: 221, file: null
groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method com.atlassian.jira.issue.fields.ImmutableCustomField#getRelevantConfig.
Cannot resolve which method to invoke for [null] due to overlapping prototypes between:

For some of the Script Post Function I do not need the Child, so in the example below it is a null. 

This now works as needed for the cascading select, bu I still have the need for multiple Components. I will mark this as Answer Accepted, and create a new question limited only to the need for a script solution for multiple Component.

Thanks again,

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.watchers.WatcherManager

def project3 = issue.getProjectObject()
def componentManager = ComponentManager.instance
def optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)

def userUtil = ComponentAccessor.getUserUtil()

MutableIssue issue = issue

def projectComponentManager3 = ComponentAccessor.getProjectComponentManager()
def newcomponent3= projectComponentManager3.findByComponentName(project3.getId(), "Security")
issue.setComponent([newcomponent3])

def customFieldManager3 = ComponentAccessor.getCustomFieldManager()
def cf3 = customFieldManager3.getCustomFieldObjectByName("SV Lead")
ApplicationUser user3 = userUtil.getUserByKey("sghodkha")
ApplicationUser user32 = userUtil.getUserByKey("")
List<ApplicationUser> newApprovers3 = []
newApprovers3.add(user3)
newApprovers3.add(user32)

def customFieldManager31 = ComponentAccessor.getCustomFieldManager()
def cf31 = customFieldManager31.getCustomFieldObjectByName("SV Management")
ApplicationUser user31 = userUtil.getUserByKey("stalukde")
ApplicationUser user312 = userUtil.getUserByKey("")
List<ApplicationUser> newApprovers31 = []
newApprovers31.add(user31)
newApprovers31.add(user312)

String SUMMARY_VALUE3 = issue.summary
String newSummary3 = "Security Vulnerability (AOS IPBG) - "+SUMMARY_VALUE3

def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()

def customFieldManager311 = ComponentAccessor.customFieldManager
def optionsManager311 = ComponentAccessor.optionsManager
def issueManager = ComponentAccessor.issueManager

def productCat311 = customFieldManager311.getCustomFieldObjectsByName("Product Category")[0]
def fieldConfig = productCat311.getRelevantConfig(issue)

def options = optionsManager311.getOptions(fieldConfig)
def parentOption311 = options.find { it.value == "IPBG" }
def childOption311 = parentOption311?.childOptions?.find { it.value == "" }

def newValues = [:]

newValues.put(null, parentOption311)
newValues.put("1", childOption311)
issue.setCustomFieldValue(productCat311, newValues)


//issue.assigneeId = ""
issue.summary = newSummary3
issue.setCustomFieldValue(cf3, newApprovers3)
issue.setCustomFieldValue(cf31, newApprovers31)

checkLink = {link -> false};
TAGS
AUG Leaders

Atlassian Community Events