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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

Edited

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

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.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Admins, notify your Jira instance of system-wide changes with the new admin announcement banner

Hi All! We’re excited to share the launch of an announcement banner that lets Jira site administrators communicate directly to their users across their  Jira Cloud instance.  ...

555 views 15 18
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you