Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to Use ScriptRunner Listener to copy selected value from Multi Select field

Paddy Byrne October 21, 2019

Hi,

I' m trying to synchronize the selected options of a Multi-Select field, across linked issues via a ScriptRunner Listner, triggered by the Issue Updated event in a named project.

The link lookup piece is working fine, but I can't crack how to actually update the Multi-select field at the other end of the link. (I can update 'single selects' using a very similar listener, so I'm certain that all works fine) The code below does actually find the options currently selected in the source Multi-select field, but can't transfer them... Based on this post I think I need to get the numeric Ids of the selected options and somehow set them? but 'options.getOptionById' creates errors and seems to be deprecated

(Environment= Jira Software Server 7.12 and SR 5.4.12)

 

Any advice welcome...

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.util.ImportUtils
import org.apache.log4j.Logger
import org.apache.log4j.Level

Issue issue = event.getIssue()
def log = Logger.getLogger("com.acme.Customlogger")
log.setLevel(Level.DEBUG)

def linkType = ["Relates"]
def userManager = ComponentAccessor.getUserManager()
def username = "jbloggs" // This user must have permissions in the target project
def user = userManager.getUserByKey(username)
def linkMgr = ComponentAccessor.getIssueLinkManager()
def CustomFieldManager cfManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()

log.debug "Starting Analysis of Multi Select field"

def cfProgram = cfManager.getCustomFieldObjects(issue).find {it.name == 'Product(s)'} // ** MultiSelect Field
def currentOptions = issue.getCustomFieldValue(cfProgram)?:[]
log.info "currentOptions: " + currentOptions

log.debug("Starting Tests")
if (issue.getIssueType().name == "Change Request") {
log.debug("Found correct issue type - starting loop..")
for (IssueLink link in linkMgr.getInwardLinks(issue.id)) {
def destIssue = link.getSourceObject() // bind to the originating source of the link
if (linkType.contains(link.issueLinkType.name)) {
destIssue.setCustomFieldValue(cfProgram, [currentOptions])
issueManager.updateIssue(user, destIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
}}}

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Douglas Johnson April 22, 2021

Paddy, 

I received some assistance from @Peter-Dave Sheehan for my question How to copy collection (select, multiselect, radio) values into sub-task(s) within a post-function?

Although my script was for a post-function, the following might be helpful.

// Single select
addCustomFieldValue(inquiryTypeField.id, inquiryTypeValue.optionId as String[]
// Multiselect
addCustomFieldValue(inquiryTypeField.id, inquiryTypeValue*.optionId as String[]

I don't know if this is considered cross-posting, but I spent so much time trying to figure this out, it should be OK include the solution here as well, IMHO.

TAGS
AUG Leaders

Atlassian Community Events