hello, I have a customfield type selection from the drop-down single value list that represents an animal species: dog or cat (two values). I would like to make sure that a second custom field, always of the single value drop-down type, shows two breeds of dogs (Dalmatian or Labrador) if I have chosen "dog" or "American cat and European cat" if I have chosen cat in the first drop-down list.
this is my groovy code (scriprunner)
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Level
import org.apache.log4j.Logger
import org.apache.log4j.Category
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.customfields.option.Options
import com.atlassian.jira.issue.fields.config.FieldConfig
import com.atlassian.jira.issue.*
import com.atlassian.jira.issue.context.IssueContextImpl
def log = Logger.getLogger("com.mypersonal.server")
log.setLevel(Level.DEBUG)
log.debug("------------- Start Behaviour Master/Slave -----------------")
def project = getFieldById("pid")
def issueType = formContents["Story"]
log.debug("---project " + project.getValue() + " issue Type " + issueType)
OptionsManager optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
//dichara due variabili che rappresentano il custom field nel form corrente di create / update
// valori originali
// FormField masterFormField = getFieldByName("MasterField")
// FormField slaveFormField = getFieldByName("SlaveField")
FormField masterFormField = getValue("MasterField")
FormField slaveFormField = getValue("SlaveField")
log.debug("Master Option Id = " + masterFormField.getFieldId())
log.debug("Slave Option Id = " + slaveFormField.getFieldId())
//valore corrente del campo master
def masterFormFieldValue = masterFormField.getValue()
//dichiara oggetto CustomField del campo Slave, partendo dall'id dell'oggetto presente sul form
def customFieldSlave = customFieldManager.getCustomFieldObject(slaveFormField.getFieldId())
log.debug("Valore Campo Master = " + masterFormFieldValue)
def issueId = formContents["id"]
def issue = ComponentAccessor.getIssueManager().getIssueObject(issueId as Long)
log.debug("--- issue ---" + issue)
def slaveFieldConfig
//se issue == null allora sono in create
if(issue == null){
def issueContext = new IssueContextImpl(project.getValue() as Long, issueType);
slaveFieldConfig = customFieldSlave.getRelevantConfig(issueContext);
} else {
slaveFieldConfig = customFieldSlave.getRelevantConfig(issue)
}
def slaveOptions = optionsManager.getOptions(slaveFieldConfig)
log.debug("Slave Options Before Processing --> "+ slaveOptions)
Map finalSlaveFieldOptions = [:]
for (Option option : slaveOptions) {
if (!option.disabled){
def optionId = Long.toString(option.getOptionId())
def optionValue = option.getValue()
finalSlaveFieldOptions.put (optionId, optionValue)
}
}
log.debug("------" + masterFormFieldValue.contains("keyword"))
//variabile unica per la rimozione di opzioni dalla lista Slave
def optionToRemove
//if (!masterFormFieldValue.contains("keyword1") || masterFormFieldValue.size() == 0 || masterFormFieldValue.contains("null")){
if(masterFormFieldValue.contains("cane")){
//rimuovi opzioni non associate a "keyword1"
optionToRemove = slaveOptions.find { it.value == "gatto europeo" }
finalSlaveFieldOptions.remove(Long.toString(optionToRemove.optionId))
optionToRemove = slaveOptions.find { it.value == "gatto siamese" }
finalSlaveFieldOptions.remove(Long.toString(optionToRemove.optionId))
slaveFormField.setFieldOptions(finalSlaveFieldOptions)
} else if(masterFormFieldValue.contains("gatto")){
//rimuovi opzioni non associate a "keyword1"
optionToRemove = slaveOptions.find { it.value == "cane dalmata" }
finalSlaveFieldOptions.remove(Long.toString(optionToRemove.optionId))
optionToRemove = slaveOptions.find { it.value == "cane labrador" }
finalSlaveFieldOptions.remove(Long.toString(optionToRemove.optionId))
slaveFormField.setFieldOptions(finalSlaveFieldOptions)
}
log.debug("Slave Options After Processing --> "+ finalSlaveFieldOptions)
Hi Kalos! I worked with your code, and this is what I got to work!
package examples
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript
import org.apache.log4j.Level
import org.apache.log4j.Logger
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.context.IssueContextImpl
@BaseScript FieldBehaviours fieldBehaviours
def log = Logger.getLogger("com.mypersonal.server")
log.setLevel(Level.DEBUG)
log.debug("------------- Start Behaviour Master/Slave -----------------")
def project = getFieldById("pid")
def issueType = formContents["Story"]
log.debug("---project " + project.getValue() + " issue Type " + issueType)
OptionsManager optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
FormField masterFormField = getFieldByName("MasterField")
FormField slaveFormField = getFieldByName("SlaveField")
log.debug("Master Option Id = " + masterFormField.getFieldId())
log.debug("Slave Option Id = " + slaveFormField.getFieldId())
def masterFormFieldValue = masterFormField.getValue()?.toString()
if (masterFormFieldValue) {
def customFieldSlave = customFieldManager.getCustomFieldObject(slaveFormField.getFieldId())
log.debug("Valore Campo Master = " + masterFormFieldValue)
def issueId = formContents["id"]
def issue = ComponentAccessor.getIssueManager().getIssueObject(issueId as Long)
log.debug("--- issue ---" + issue)
def slaveFieldConfig
if (issue == null) {
def issueContext = new IssueContextImpl(project.getValue() as Long, issueType);
slaveFieldConfig = customFieldSlave.getRelevantConfig(issueContext);
} else {
slaveFieldConfig = customFieldSlave.getRelevantConfig(issue)
}
def slaveOptions = optionsManager.getOptions(slaveFieldConfig)
log.debug("Slave Options Before Processing --> " + slaveOptions)
Map finalSlaveFieldOptions = [:]
for (Option option : slaveOptions) {
if (!option.disabled) {
def optionId = Long.toString(option.getOptionId())
def optionValue = option.getValue()
finalSlaveFieldOptions.put(optionId, optionValue)
}
}
log.debug("------" + masterFormFieldValue.contains("keyword"))
def optionToRemove
if (masterFormFieldValue.contains("cane")) {
optionToRemove = slaveOptions.find { it.value == "gatto europeo" }
finalSlaveFieldOptions.remove(Long.toString(optionToRemove.optionId))
optionToRemove = slaveOptions.find { it.value == "gatto siamese" }
finalSlaveFieldOptions.remove(Long.toString(optionToRemove.optionId))
slaveFormField.setFieldOptions(finalSlaveFieldOptions)
} else if (masterFormFieldValue.contains("gatto")) {
optionToRemove = slaveOptions.find { it.value == "cane dalmata" }
finalSlaveFieldOptions.remove(Long.toString(optionToRemove.optionId))
optionToRemove = slaveOptions.find { it.value == "cane labrador" }
finalSlaveFieldOptions.remove(Long.toString(optionToRemove.optionId))
slaveFormField.setFieldOptions(finalSlaveFieldOptions)
}
log.debug("Slave Options After Processing --> " + finalSlaveFieldOptions)
}
I think the main issue was here: it should be getFieldByName instead of getValue.
FormField masterFormField = getFieldByName("cat/dog test")
FormField slaveFormField = getFieldByName("cat/dog2")
I also put the bulk of the code inside of an if statement as a validator to make sure the code only runs if there actually is a value inside of masterFormFieldValue.
hello @Carmen Creswell [Adaptavist] and thank you for answering me, I took inspiration from your example code and then I still made some changes after that.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.