I am trying to take an incoming single line custom field value and insert it into several other single choice select list custom field values based on conditions. Any help would be appreciated.
Logic Being Requested:
Let me see if I understand... you want to have a JSM form to allow user to submit updates to some Custom Field options.
You probably want to run this from a scriptrunner postfunction.
If so, something like this might work:
import com.atlassian.jira.issue.customfields.option.LazyLoadedOptionimport com.atlassian.jira.component.ComponentAccessorimport com.onresolve.scriptrunner.runner.customisers.WithPluginimport com.onresolve.scriptrunner.runner.customisers.PluginModuleimport com.atlassian.servicedesk.api.requesttype.RequestTypeServiceimport com.atlassian.servicedesk.api.ServiceDeskManager@WithPlugin("com.atlassian.servicedesk")@PluginModule RequestTypeService requestTypeService@PluginModule ServiceDeskManager serviceDeskManagerdef cfm = ComponentAccessor.customFieldManagerdef om = ComponentAccessor.optionsManager//this map here is your critical logic ... //it maps the values selected in the Vendor Type field with each Single Select that must be updated//you could easily add new option/fields if you needed todef vendorTypeFieldMap = [ 'Payroll': ['Vendor Name','Payroll Vendor/s',''], 'HRIS': ['Vendor Name','HRIS Provider/s','Prior Administrator'], 'COBRA': ['Vendor Name','COBRA Administrator'], 'Direct Bill': ['Vendor Name','Direct Bill Administrator']]//custom fields objects that we'll usedef requestTypeCf = cfm.getCustomFieldObjectsByName('Customer Request Type')[0] def vendorTypeCf = cfm.getCustomFieldObjectsByName('Vendor Type')[0]def vendorNameCf = cfm.getCustomFieldObjectsByName('Vendor Name - Actual')[0]//raw values from those custom fields in the correct typedef requestTypeKey = issue.getCustomFieldValue(requestTypeCf) as Stringdef newVendorType = issue.getCustomFieldValue(vendorTypeCf) as LazyLoadedOptiondef newVendorName = issue.getCustomFieldValue(vendorNameCf) as String//the Customer Request Type custom field returns the Request Type key which is GUID number//first we need the service desk objectdef serviceDesk = serviceDeskManager.getServiceDeskForProject(issue.projectObject)//then we build a requestType querydef queryBuilder = requestTypeService.newQueryBuilder()queryBuilder.requestOverrideSecurity(true)queryBuilder.serviceDesk(serviceDesk.id)//fetch all the request types and filter for the one that match the keydef requestType = requestTypeService.getRequestTypes(null, queryBuilder.build()).find{requestTypeKey.contains(it.properties.key as String)}//the main logic starts hereif(requestType.name == 'Add New Vendor'){ //find the map item that matches the selection and loop through each fieldname to update vendorTypeFieldMap[newVendorType.value].each{targetFieldName -> def targetField = cfm.getCustomFieldObjectsByName(targetFieldName)[0] def config = targetField.getRelevantConfig(issue) def options = om.getOptions(config) if(!options.any{it.value == newVendorName}){ om.createOption(config, null, 0, newVendorName) //reload the options including the new one options = om.getOptions(config) //sort them and update the sequence options.sort{it.value}.eachWithIndex{opt, i-> opt.sequence = i } //store the new sequence om.updateOptions(options) } } }
@PD Sheehan, thank you so much for the quick response to this. Unfortunately, we do not have Service Desk as a result I am getting the following error:
The script could not be compiled:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: General error during conversion: Requested plugin com.atlassian.servicedesk was not installed/enabled, compiling source unit Script38.groovy.
Is there any other method to extract the Vendor Request type selection?
Everything else look like it would work.
Thank you for the help!
ah... then if vendor request type is a standard "single select" field, then we can simplify the script
import com.atlassian.jira.issue.customfields.option.LazyLoadedOptionimport com.atlassian.jira.component.ComponentAccessordef cfm = ComponentAccessor.customFieldManagerdef om = ComponentAccessor.optionsManager//this map here is your critical logic ... //it maps the values selected in the Vendor Type field with each Single Select that must be updated//you could easily add new option/fields if you needed todef vendorTypeFieldMap = [ 'Payroll': ['Vendor Name','Payroll Vendor/s',''], 'HRIS': ['Vendor Name','HRIS Provider/s','Prior Administrator'], 'COBRA': ['Vendor Name','COBRA Administrator'], 'Direct Bill': ['Vendor Name','Direct Bill Administrator']]//custom fields objects that we'll usedef requestTypeCf = cfm.getCustomFieldObjectsByName('Vendor Request Type')[0] def vendorTypeCf = cfm.getCustomFieldObjectsByName('Vendor Type')[0]def vendorNameCf = cfm.getCustomFieldObjectsByName('Vendor Name - Actual')[0]//raw values from those custom fields in the correct typedef requestType = issue.getCustomFieldValue(requestTypeCf) as LazyLoadedOptiondef newVendorType = issue.getCustomFieldValue(vendorTypeCf) as LazyLoadedOptiondef newVendorName = issue.getCustomFieldValue(vendorNameCf) as String//the main logic starts hereif(requestType.value == 'Add New Vendor'){ //find the map item that matches the selection and loop through each fieldname to update vendorTypeFieldMap[newVendorType.value].each{targetFieldName -> def targetField = cfm.getCustomFieldObjectsByName(targetFieldName)[0] def config = targetField.getRelevantConfig(issue) def options = om.getOptions(config) if(!options.any{it.value == newVendorName}){ om.createOption(config, null, 0, newVendorName) //reload the options including the new one options = om.getOptions(config) //sort them and update the sequence options.sort{it.value}.eachWithIndex{opt, i-> opt.sequence = i } //store the new sequence om.updateOptions(options) } } }
@PD Sheehan I am receiving the following error when executed in post function.
2021-07-19 15:09:09,423 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed on issue CO-6 for user 'shiggins'. org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[Payroll, HRIS, COBRA, Direct Bill]' with class 'java.util.ArrayList' to class 'com.atlassian.jira.issue.customfields.option.LazyLoadedOption' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: com.atlassian.jira.issue.customfields.option.LazyLoadedOption(com.atlassian.jira.issue.customfields.option.LazyLoadedOption, com.atlassian.jira.issue.customfields.option.LazyLoadedOption, com.atlassian.jira.issue.customfields.option.LazyLoadedOption, com.atlassian.jira.issue.customfields.option.LazyLoadedOption)at Script40.run(Script40.groovy:24)
It looks like you're new here. Sign in or register to get started.