Set Select List Custom field from scriptrunner clone issue postfunction.

Vineet Kumar July 9, 2018

Hello Everyone, We are using Jira 7.6.4 and Scriptrunner 5.4.11, The task is to autopopulate a Selectlist customfield in Jira on the target issue. I have tried variuos scripts from the blog but none of them are working for me. Is something changed on Scriptrunner plugin recently? Can someone help me to get the correct script including the correct packages. 

 

I have tried the below scripts:

 

CustomField customField = customFieldManager.getCustomFieldObject("customfield_17100");

OptionsManager optManager = ComponentAccessor.getOptionsManager();
Options options = optManager.getOptions(customField.getRelevantConfig(issue), null));

Option newOption = options.getOptionById("Campaign"); // Check your option id

ModifiedValue mVal = new ModifiedValue(issue.getCustomFieldValue(customField), newOption );

customField.updateValue(null, issue, mVal, new DefaultIssueChangeHolder());

 

===============*=======================

 

import com.atlassian.jira.component.ComponentAccessor
def cfSelect = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Solution")
def cfConfig = selectCf.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.toString() == 'Campaign'
}
issue.setCustomFieldValue(cfSelect, value)

 

 

1 answer

0 votes
Mark Markov
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.
July 9, 2018

Hello @Vineet Kumar

try this

 

import com.atlassian.jira.component.ComponentAccessor
def cfSelect = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Solution")
def cfConfig = selectCf.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.value == 'Campaign'
}
issue.setCustomFieldValue(cfSelect, value)
Vineet Kumar July 9, 2018

Thanks for the response Mark, I have already tried the above option, I am getting the below error.

 

Caused by: java.lang.ClassCastException: com.atlassian.jira.issue.customfields.option.LazyLoadedOption cannot be cast to java.util.Collection
at com.atlassian.jira.issue.customfields.impl.AbstractMultiCFType.createValue(AbstractMultiCFType.java:39) [jira-api-7.6.4.jar:?]
at com.atlassian.jira.workflow.function.issue.IssueCreateFunction.execute(IssueCreateFunction.java:81) [classes/:?]
at com.opensymphony.workflow.AbstractWorkflow.executeFunction(AbstractWorkflow.java:1014) [osworkflow-2.9.0-atlassian-1.jar:2.9.0-atlassian-1]
at com.opensymphony.workflow.AbstractWorkflow.transitionWorkflow(AbstractWorkflow.java:1407) [osworkflow-2.9.0-atlassian-1.jar:2.9.0-atlassian-1]
at com.opensymphony.workflow.AbstractWorkflow.initialize(AbstractWorkflow.java:606) [osworkflow-2.9.0-atlassian-1.jar:2.9.0-atlassian-1]
at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(OSWorkflowManager.java:742) [classes/:?]

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

Try enclosing the value within a collection. 

import com.atlassian.jira.component.ComponentAccessor
def cfSelect = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Solution")
def cfConfig = selectCf.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.value == 'Campaign'
}
issue.setCustomFieldValue(cfSelect, [value])

I did something similar and it worked for me. 
This is what I have:

 Issue newIssueObject = ComponentAccessor.getIssueManager().getIssueByKeyIgnoreCase(newIssueID.toString().trim())

def customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField targetProjectBoardField = customFieldManager.getCustomFieldObject("customfield_10144")
String targetProjectBoardFieldValue = newIssueObject.getCustomFieldValue(targetProjectBoardField)

log.info "Current Project Board Value for target issue ("+newIssueID.toString().trim()+") is:" +targetProjectBoardFieldValue


if(targetProjectBoardFieldValue!=null && !targetProjectBoardFieldValue.equals("") && targetProjectBoardFieldValue.length()>1){

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
targetProjectBoardField.updateValue(null, newIssueObject, mVal, changeHolder)

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

 Modify per your needs.

Like # people like this
Suji Shyam August 27, 2020

Thank you so much @Gundlupet Sreenidhi .

The below script works fine.

import com.atlassian.jira.component.ComponentAccessor
def cfSelect = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Solution")
def cfConfig = selectCf.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.value == 'Campaign'
}
issue.setCustomFieldValue(cfSelect, [value])
Like # people like this

Suggest an answer

Log in or Sign up to answer