Forums

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

Text field lookup to determine if a new value needs to be added to a single select field

Maria DePasquale
Contributor
August 19, 2020

On issue Create, I need to look up the value in a text field and see if the value already exists in a single select drop down field.  If the value exists, I need to set the value in the single select.

If the value doesn't exist, I first need to add the value to the single select drop down, then I need to select the new value. 

Based on my research, this can be done using a Listener. 

So I found a sample script listener and set it up and modified it for my situation:

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.changehistory.ChangeHistoryItem
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.CustomField
import com.atlassian.jira.issue.fields.Field
import com.atlassian.jira.issue.fields.config.FieldConfig
import com.atlassian.jira.user.ApplicationUser
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.onresolve.scriptrunner.parameters.annotation.FieldPicker
@FieldPicker(label = "Vendor", description = "Vendor")
Field dropdownField
@FieldPicker(label = "Coupa Supplier", description = "Coupa Supplier")
Field textField

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField cfTextField = customFieldManager.getCustomFieldObject(textField.getId())

Issue issue = event.getIssue()

//Checks if the Text Field is changed in the current "Issue Updated" Event
if (cfTextField.getName() in getFieldsChanged()*.getField()) {

String textFieldValue = issue.getCustomFieldValue(cfTextField) as String

//Checks if the Text Field have Text (it may be blank)
if (textFieldValue) {

OptionsManager optionsManager = ComponentAccessor.getOptionsManager()
CustomField cfDropdown = customFieldManager.getCustomFieldObject(dropdownField.getId())
FieldConfig dropdownFieldConfig = cfDropdown.getRelevantConfig(issue)
Options dropdownCurrentOptions = optionsManager.getOptions(dropdownFieldConfig)
Boolean isTextFieldValueInDropdownOptions = textFieldValue.toLowerCase() in dropdownCurrentOptions*.getValue()*.toLowerCase()
Option option

//Checks if the Text field value is currently in the dropdown options
//If not, creates a new option with the value from the Text Field
//If it's already in the current options just set the dropdown field with that option
//In both cases removes the Text Field's value
if (!isTextFieldValueInDropdownOptions) {
long newSeqId = dropdownCurrentOptions*.getSequence().max() - 1
option = optionsManager.createOption(dropdownFieldConfig, null, newSeqId, textFieldValue)
} else {
option = dropdownCurrentOptions.find { it.getValue() == textFieldValue }
}

updateIssue(issue, cfDropdown, cfTextField, option)
}
}

List<ChangeHistoryItem> getFieldsChanged() {
return ComponentAccessor.getChangeHistoryManager().getAllChangeItems(event.getIssue())?.findAll {
it?.getChangeGroupId() == event.getChangeLog()?.id
}
}

void updateIssue(Issue issue, CustomField cfDropdown, CustomField cfTextField, Option option) {

IssueService issueService = ComponentAccessor.getIssueService()
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters()

issueInputParameters.with {
addCustomFieldValue(cfDropdown.getIdAsLong(), option.getOptionId().toString())
addCustomFieldValue(cfTextField.getIdAsLong(), null)
}

IssueService.UpdateValidationResult updateValidationResult = issueService.validateUpdate(currentUser, issue.getId(), issueInputParameters)
if (updateValidationResult.isValid()) {
issueService.update(currentUser, updateValidationResult)
} else {
log.warn("Failed to update issue: ${issue.getKey()}: ${updateValidationResult.getErrorCollection()}")
}
}

 

However, I am getting the following error:

2020-09-16 11:57:01,396 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script463.groovy: 16: unable to resolve class com.onresolve.scriptrunner.parameters.annotation.FieldPicker
 @ line 16, column 1.
   import com.onresolve.scriptrunner.parameters.annotation.FieldPicker

 

 

 

 

0 answers

Suggest an answer

Log in or Sign up to answer