Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

JIRA7 Script Runner Cascade Field - Default Configuration Scheme How to Set Default Value

Ramakrishnan Srinivasan
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.
January 26, 2019

Hi,

I need some help in setting default value for a cascade field in Default Configuration Scheme (not in an issue) after I add new parent and child options to that cascade field

 I have this code working to add a new parent option and child options to a cascade field. What is not working for me is, after adding the options, if I want to set the new parent and child as the Default value for that cascade field in Default Configuration Scheme (not in an issue but the field configuration itself)

on line cascSelectCf.setFormValue([defaultParentOption.optionId, defaultChildOption.optionId]) I get the error 

groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.fields.ImmutableCustomField.setFormValue() is applicable for argument types: (java.util.ArrayList) values: [[13565, 13567]] at Script2077.run(Script2077.groovy:49)

This setFormValue code I took from  https://scriptrunner.adaptavist.com/latest/jira/recipes/behaviours/setting-default-fields.html and I think I am misunderstanding, may be this behavior is for front end and for issues. But I need it for field config. 

//https://community.atlassian.com/t5/Jira-questions/Help-with-script-to-add-new-Cascading-Select-parent-and-child/qaq-p/960283

import org.apache.log4j.Logger
import org.apache.log4j.Level
log.setLevel(Level.DEBUG)

import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.option.Options
import com.atlassian.jira.issue.customfields.option.*
import com.onresolve.jira.groovy.user.FormField

def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def cascSelectCf = customFieldManager.getCustomFieldObjectByName("DUMMY_ONE")
// get an issue just for relevant config
def issue = issueManager.getIssueObject("PROJ-1")
def fieldConfig = cascSelectCf.getRelevantConfig(issue)

Options options = optionsManager.getOptions(cascSelectCf.getRelevantConfig(issue));

// the parent option I want to add
String parentOptionValue = "Feb-19"

def rootOptions = options.getRootOptions()
def rootOptionsNew
if (!parentAlreadyPresent(rootOptions, parentOptionValue)) {
optionsManager.createOption(fieldConfig, null, (long) 0, parentOptionValue);
// Set optionsNew to get immediately the newly created parentOptionID and create children
Options optionsNew = optionsManager.getOptions(cascSelectCf.getRelevantConfig(issue));
rootOptionsNew = optionsNew.getRootOptions()
def parentOption = getExistingParent(rootOptionsNew, parentOptionValue)
def parentOptionID = parentOption.optionId
optionsManager.createOption(fieldConfig, parentOptionID, (long) 100, "25%")
optionsManager.createOption(fieldConfig, parentOptionID, (long) 100, "50%")
optionsManager.createOption(fieldConfig, parentOptionID, (long) 100, "75%")
optionsManager.createOption(fieldConfig, parentOptionID, (long) 100, "100%")
optionsManager.createOption(fieldConfig, parentOptionID, (long) 100, "0%")
}

//to set the default to newly created parent option and one of the child

def defaultParentOption = optionsNew.find { it.value == parentOptionValue }
def defaultChildOption = defaultParentOption?.childOptions?.find { it.value == "50%" }

//IT FAILS HERE
cascSelectCf.setFormValue([defaultParentOption.optionId, defaultChildOption.optionId])

rootOptionsNew.each {
if(it.value == parentOptionValue) {
log.info "$it.optionId - $it.value"
}
}

boolean parentAlreadyPresent(Options rootOptions, String parentOptionValue) {
for (Option o:rootOptions) {
if (o.getValue().equals(parentOptionValue)) {
return true;
}
}
return false;
}

def getExistingParent(Options rootOptions, String parentOptionValue) {
for (Option o : rootOptions) {
if (o.getValue().equals(parentOptionValue)) {
return o;
}
}

}

//UPDATE
//https://community.atlassian.com/t5/Marketplace-Apps-questions/How-to-update-select-list-cascade-with-post-function-Script/qaq-p/783955

//DELETE
//rootOptions.each {
//optionsManager.deleteOptionAndChildren(it)
//}

//DISABLE
//rootOptions.each {
//optionsManager.disableOption(it)
//}

//ENABLE
//rootOptions.each {
//optionsManager.enableOption(it)
//}

 Any help is appreicated, thank you in advance

 

with warm regards

ramki

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events