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

Scriptrunner - set value on multiselect custom field

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

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

 

ModifiedValue mVal = new ModifiedValue(newIssueObject.getCustomFieldValue(targetProjectBoardField), [newOption]);

Hello Sree,

can you please paste the full script?

thank you

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
TAGS

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