Hi folks,
I'm not a developer and would love some input - my head is spinning at this point.
My use case is the following:
If a dropdown custom field has the value "Regression Test", then check the box on the "Add to Regression Set" checkbox.
I put a post function on the create step as the last step. I had the following code strung together from multiple examples and even though it wasn't failing, it wasn't checking the box.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.issue.customfields.manager.OptionsManager
log.setLevel(org.apache.log4j.Level.DEBUG)
final customFieldName = "Test Type"
//Get custom field issue with this name
def customFieldManager = ComponentAccessor.customFieldManager
def customField = customFieldManager.getCustomFieldObjects(issue).findByName(customFieldName)
assert customField: "Could not find custom field with name $customFieldName"
def customFieldVal = issue.getCustomFieldValue(customField) as LazyLoadedOption
log.debug "Test Type is ${customFieldVal}"
//set "Add to Regression Set' as field to be updated- all from my data conversion issue type when updating checkbox fields
def optionsManager = ComponentAccessor.getComponent(OptionsManager)
def customFieldManager2 = ComponentAccessor.getCustomFieldManager()
//Update "Add to Regression Set" depending on the custom field value
switch (customFieldVal?.value) {
case "Regression Test":
log.debug "Add to Regression Set checkbox should be checked"
def cf = customFieldManager2.getCustomFieldObjectByName("Add to Regression Set") //[0]
def fieldConfig = cf.getRelevantConfig(issue)
def options = optionsManager.getOptions(fieldConfig).getOptionForValue("Yes", null)
//def optionsToSet = options.findAll {it.value in ["Yes",null]}
issue.setCustomFieldValue(cf, options)
log.debug "Add to Regression Set value is ${options}"
log.debug "Add to Regression Set checkbox was checked"
break
default:
log.debug "No need to update the Add to Regression Set checkbox"
break
}
I then found this post: ScriptRunner Modifying Checkbox values on transition post-function
and thought maybe my code could be a whole lot more simple, but when I use the following,
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfig
def customFieldManager = ComponentManager.getInstance().getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Add to Regression Set")
def fieldConfig = cf.getRelevantConfig(issue)
value = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Yes' }
if (cfValues['Test Type']?.value == 'Regression Test') {
issue.setCustomFieldValue(cf, value)
}
I get an error before I can save on import com.atlassian.jira.ComponentManager saying to resolve the issue class.
I'd love some help. I thought this would be easier than it has turned out to be.
(That's always the case...)
Thanks!!
Laurie