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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,551,958
Community Members
 
Community Events
184
Community Groups

Set Select List Custom field from scriptrunner clone issue postfunction.

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.
Jul 09, 2018 • edited

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)

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.
Oct 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

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])

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events