Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Compare value in User Picker custom fields

Kelvin Teng April 13, 2022

Hi,

Appreciate some guidance from experts in the community on how to write a Behaviour script to compare 2 user picker custom fields to ensure they are not the same users.

For example, in User Picker A, User A is selected. In User Picker B, User A cannot be selected again and returns an error.

Thank you in advanced.

Kind regards,

Kelvin

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 13, 2022

Hi Kelvin

That's a fairly basic use case.

def fieldA = getFieldByName('User Picker A')
def fieldB = getFieldByName('User Picker B')
def currentField = getFieldById(fieldChanged)

fieldA.clearError()
fieldB.clearError()

if(fieldA.value == fieldB.value){
currentField.setError("Please select a different user. $currentField.value was already used.")
}

Put this script in the server-side script for both fields.

It will set an error message in the most recently modified field and clear the error if either fields are corrected.
Adjust for your actual field names (of change to getFieldById('customfield_xxxxx'))

Kelvin Teng April 19, 2022

Hi Peter.

Thank you so much for the assist. I've tested the script and it is working perfectly.

Kind regards,

Kelvin

Kelvin Teng April 25, 2022

Hi Peter,

Just a follow up question, I noticed when both fields are empty the error would appear. Is there a way for the error not to appear when both fields are empty?

Thanks.

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 26, 2022

If you only want to validate when the fields are not empty, you can add some logic like this:

if(fieldA.value && fieldB.value){
if
(fieldA.value == fieldB.value){
currentField.setError("Please select a different user. $currentField.value was already used.")
}
}

or combine it:

if(fieldA.value && fieldB.value && fieldA.value == fieldB.value){
currentField.setError("Please select a different user. $currentField.value was already used.")
}
0 votes
Артем Балашов August 10, 2022

Good afternoon. I would be very grateful for help with the question of how to compare the user selection field with the multiple selection of users. and if there is a match, issue an error message. cannot be done on its own.
an example of an article where the fields are compared with the user's choice.

Артем Балашов August 15, 2022

Here thus I try to compare on matches of a field of a multiple choice of users. but i lack skills

import com.atlassian.jira.component.ComponentAccessor

// Get a pointer to my custom fields
def SelectList1 = getFieldByName("customfieldname")
def SelectList2 = getFieldByName("customfieldname2")

def currentField = getFieldById( fieldChanged )

SelectList1.clearError()
SelectList2.clearError()

// Get the Value of My Select Field
def selectVal1 = SelectList1.getValue() as String
def selectVal2 = SelectList2.getValue() as String

// If the first option is selected in the multi select list then make the text field required.
SelectList1.getValue().each {
if(selectVal1.contains(selectVal1) == selectVal2.contains(selectVal2)) {
currentField.setError( "Пожалуйста, выберите другого пользователя. Значение $currentField. уже было использовано." )}
}

TAGS
AUG Leaders

Atlassian Community Events