Scriptrunner - set value on multiselect custom field

Gundlupet Sreenidhi
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.
October 18, 2018

I am attempting to set the value on a multiselect custom field through scriptrunner.

I have tried multiple options based on suggestions from the community - however none of them seem to work properly. 

log.info "Updating Target Issue Project Board"
OptionsManager optManager = ComponentAccessor.getOptionsManager();
Options options = optManager.getOptions(targetProjectBoardField.getRelevantConfig(newIssueObject));
log.info "Available Options: " + options
Option newOption = options.getOptionById(Long.parseLong(affectedBoardsFieldValue));
log.info "Identified Option: " + newOption


def fieldConfig = targetProjectBoardField.getRelevantConfig(newIssueObject)
def value = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.optionId.toString().trim() == affectedBoardsFieldValue.toString().trim() }

log.info "Final Value:" + value

def changeHolder = new DefaultIssueChangeHolder()
def issueCustomFieldValues = newIssueObject.getCustomFieldValue(targetProjectBoardField)
log.info "issueCustomFieldValues: "+issueCustomFieldValues
ModifiedValue mVal = new ModifiedValue(issueCustomFieldValues, value)
log.info "mVal: "+mVal

newIssueObject.setCustomFieldValue(targetProjectBoardField, [value])
newIssueObject.store()

log.info "Target Issue Project Board has been updated."
log.info "Validation - " + newIssueObject.getCustomFieldValue(targetProjectBoardField)

This shows the following log:

2018-10-18 14:23:28,126 INFO [jira.groovy]: Updating Target Issue Project Board

2018-10-18 14:23:28,126 INFO [jira.groovy]: Available Options: [AAA, BBB]

2018-10-18 14:23:28,126 INFO [jira.groovy]: Identified Option:AAA

2018-10-18 14:23:28,128 INFO [jira.groovy]: Final Value:Agile Workforce

2018-10-18 14:23:28,128 INFO [jira.groovy]: issueCustomFieldValues: [XXX]

2018-10-18 14:23:28,128 INFO [jira.groovy]: mVal:com.atlassian.jira.issue.ModifiedValue@ecdf7a

2018-10-18 14:23:28,129 INFO [jira.groovy]: Target Issue Project Board has been updated.

2018-10-18 14:23:28,129 INFO [jira.groovy]: Validation - [AAA]

 

The log shows that the issue is being updated successfully, However when I go to the issue, it still reflects the old value XXX

 

 Another options is:

log.info "Updating Target Issue Project Board"
OptionsManager optManager = ComponentAccessor.getOptionsManager();
Options options = optManager.getOptions(targetProjectBoardField.getRelevantConfig(newIssueObject));
log.info "Available Options: " + options
Option newOption = options.getOptionById(Long.parseLong(affectedBoardsFieldValue));
log.info "Identified Option: " + newOption

ModifiedValue mVal = new ModifiedValue(newIssueObject.getCustomFieldValue(targetProjectBoardField), newOption);
targetProjectBoardField.updateValue(null, newIssueObject, mVal, new DefaultIssueChangeHolder());


log.info "Target Issue Project Board has been updated."
log.info "Validation - " + newIssueObject.getCustomFieldValue(targetProjectBoardField)


 

This throws the following error:

ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: JIRA-849, actionId: 111, file: <inline script>
java.lang.ClassCastException: com.atlassian.jira.issue.customfields.option.LazyLoadedOption cannot be cast to java.util.Collection
at com.atlassian.jira.issue.customfields.impl.MultiSelectCFType.valuesEqual(MultiSelectCFType.java:90)
at com.atlassian.jira.issue.fields.ImmutableCustomField.valuesEqual(ImmutableCustomField.java:1575)
at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:424)
at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)
at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source)

Can someone help me fix this and get it working properly?

 

2 answers

1 accepted

1 vote
Answer accepted
Gundlupet Sreenidhi
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.
October 18, 2018

I found it - enclosing the newOption within a collection did the trick.

 

ModifiedValue mVal = new ModifiedValue(newIssueObject.getCustomFieldValue(targetProjectBoardField), [newOption]);
0 votes
gagan m s February 28, 2019

Hello Sree,

can you please paste the full script?

thank you

Gundlupet Sreenidhi
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.
April 29, 2019
log.info "Updating Target Issue Project Board"
OptionsManager optManager = ComponentAccessor.getOptionsManager();
Options options = optManager.getOptions(targetProjectBoardField.getRelevantConfig(newIssueObject));
log.info "Available Options: " + options
Option newOption = options.getOptionById(Long.parseLong(affectedBoardsFieldValue));
log.info "Identified Option: " + newOption


def fieldConfig = targetProjectBoardField.getRelevantConfig(newIssueObject)
def value = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.optionId.toString().trim() == affectedBoardsFieldValue.toString().trim() }

log.info "Final Value:" + value

def changeHolder = new DefaultIssueChangeHolder()
def issueCustomFieldValues = newIssueObject.getCustomFieldValue(targetProjectBoardField)
log.info "issueCustomFieldValues: "+issueCustomFieldValues
ModifiedValue mVal = new ModifiedValue(newIssueObject.getCustomFieldValue(targetProjectBoardField),[newOption])
log.info "mVal: "+mVal

newIssueObject.setCustomFieldValue(targetProjectBoardField, [value])
newIssueObject.store()

log.info "Target Issue Project Board has been updated."
log.info "Validation - " + newIssueObject.getCustomFieldValue(targetProjectBoardField)
Like # people like this

Suggest an answer

Log in or Sign up to answer