how do I compare a dropdown to a text field?

Jacob francois June 15, 2020

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()def optionsManager = ComponentAccessor.getOptionsManager()def faveFruitOtherFld = customFieldManager.getCustomFieldObjectByName("Add New Vendor Name")def issueService = ComponentAccessor.getIssueService()def faveFruitFld = customFieldManager.getCustomFieldObjectByName("Vendor Name")

  def issue = event.issuedef otherFaveFruit = issue.getCustomFieldValue(faveFruitOtherFld) as String

import com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.issue.comments.CommentManagerimport com.atlassian.jira.user.ApplicationUserimport com.onresolve.jira.groovy.user.FieldBehaviours


import com.atlassian.jira.component.ComponentAccessor

final String groupName = "Procurement"


int numOfVendors = faveFruitFld.size()

for( int i = 0; i < numOfVendors ; i++ )
{        if (faveFruitFld[i] == faveFruitFld){            }    else    {              if (issue.reporter ? ComponentAccessor.groupManager.isUserInGroup(issue.reporter, groupName): true) {    
if (otherFaveFruit)             {    
    def fieldConfig = faveFruitFld.getRelevantConfig(issue)    def currentOptions = optionsManager.getOptions(fieldConfig)    def newSeqId = currentOptions*.sequence.max() - 1    def option = optionsManager.createOption(fieldConfig, null, newSeqId, otherFaveFruit)
    def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()    def issueInputParameters = issueService.newIssueInputParameters()    issueInputParameters.with {        addCustomFieldValue(faveFruitFld.idAsLong, option.optionId.toString())        addCustomFieldValue(faveFruitOtherFld.idAsLong, null)                    }    def updateValidationResult = issueService.validateUpdate(currentUser, issue.id, issueInputParameters)    if (updateValidationResult.isValid()) {        issueService.update(currentUser, updateValidationResult)    } else {        log.warn("Failed to update issue: ${issue.key}: ${updateValidationResult.errorCollection}")    } }


ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().loggedInUser
def wertreporter = issue.reporter.name
def create = wertreporter + " added " + otherFaveFruit +" to the vendor dropdown "
CommentManager commentMgr = ComponentAccessor.getCommentManager()
//Der wirkliche KommentarcommentMgr.create(issue, currentUser, create, true)        }            }}


the above code is my attempt. 

 

I started with 

https://scriptrunner.adaptavist.com/5.6.15/jira/recipes/behaviours/select-list-other.html

 

how do I "check that the user-provided other value doesn’t actually already exist in the list"?


    

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
June 15, 2020

Hi @Jacob francois here you go, explained:

First you have to put this code in the Behaviours "Dropdown" Field:

Select your text field in the selector and don't forget to change the "Other" option with your field "Other" option (maybe in ahother language) in the code.

import com.atlassian.jira.issue.fields.Field
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.onresolve.scriptrunner.parameters.annotation.FieldPicker
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

@FieldPicker(label = "Text field", description = "Your text field")
Field textField

FormField ffDropdown = getFieldById(getFieldChanged())
FormField ffTextField = getFieldById(textField.getId())

String selectedOption = ffDropdown.getValue() as String
Boolean isOtherSelected = selectedOption == "Other"

ffTextField.setHidden(!isOtherSelected)
ffTextField.setRequired(isOtherSelected)

Second, you have to add the following code in a new Script Listener. Don't forget to select the project, the "Issue Updated" Event, the Dropdown field and the Text Field that you are using.

The code is commented to better understanding of what is doing:

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.parameters.annotation.FieldPicker

@FieldPicker(label = "Dropdown field", description = "Your dropdown field")
Field dropdownField
@FieldPicker(label = "Text field", description = "Your text field")
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()}")
}
}

 I hope this is what your looking for.
Regards

Jacob francois June 24, 2020

thank you. I am not sure why this script isn't working. I'm not getting any errors via the listener screen. 

0 votes
František Špaček _MoroSystems_
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.
June 15, 2020

Hello Jacob,

 

could you please post script in code block so new lines are not altered? This way, code is hardly readable a understandable. Plus, could you please specify what you problem is (what errors you get) and when is the expected result?

 

Thank you and have a nice day!

TAGS
AUG Leaders

Atlassian Community Events