How to get Multiple values from Edit Fields to validate users

ChristopherChilds
Contributor
October 4, 2024

Hello,

I am wondering if there was a way to validate information on an Edit Screen before Saving changes.

I have tree User Picker Fields. I want to validate that the same user does not exist more than once. The problem i I have is how to get the values from three different fields on the from the Edit UI.

I have seen references to the method getFieldChanged()
https://docs.adaptavist.com/sr4js/6.26.0/features/behaviours/api-quick-reference

Used as follows:

 

getFieldById(getFieldChanged())

 

But this is only mapped to the Field in a behaviour. It is not possible to use this to get the value of three different fields and then compare. 

Another way I have seen is which appeared to promise is getFormValue()

I tried to use as follows:

 

def developerField = getFieldById("customfield_0000000")
def developerUser = developerField.getFormValue() as String
but the value is null.

Does anyone have any suggestions?

1 answer

0 votes
Sergei Troshin
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.
October 6, 2024

Hi @ChristopherChilds

The tricky part here to place server-side script on each User Picker field in Beahviours.

For example, i have 3 user picker fields:

screen1.png
with IDs customfield_12300, customfield_12301 and customfield_12302.

Then i create behaviour and paste the same code on each:
screen2.png

This method capturing changes in each field and procced comparing and alerting

screen3.png

Hope it will help you! Let me know if you occure some problems or accept answer if not.

ChristopherChilds
Contributor
October 8, 2024

Thank you Sergy.

 

I think this is about half right. Though there is one problem with this solution. If I am to change users against the roles.

For example.

in the below image I have the users Red User, Orange User and Green User. If I update Change Owner from the Orange User to the Red User then I get the exception because when I make the change the Developer has the same value.
When I next update the Developer from the Red User to the Orange the Error message persists.

I am not sure if, with scriptrunner, this is actually possible. I was hoping that there was a method which would actually just validate the users when attempting to update an issue.

ChangeUsersnames.png

Sergei Troshin
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.
October 8, 2024

Hi @ChristopherChilds 

Thank you for your response!

I shared a small trick earlier, but not a complete script that meets your needs. If you would like, I've written some code that might help. Please feel free to try pasting it into each server-side script of the User Picker Fields in Behaviors. I hope this is helpful!

screen5.png

def userPicker1 = getFieldById("customfield_12300")?.getValue() // Change owner
def userPicker2 = getFieldById("customfield_12301")?.getValue() // Developer
def userPicker3 = getFieldById("customfield_12302")?.getValue() // Tester

def
errorMessage = "Error Duplicate Users."

def userPickers = [
    [value: userPicker1, fieldId: "customfield_12300"],
    [value: userPicker2, fieldId: "customfield_12301"],
    [value: userPicker3, fieldId: "customfield_12302"]

]
def groupedByValue = userPickers.groupBy { it.value }

groupedByValue.each { value, entries ->
    if (value != null && entries.size() > 1) {
        entries.each { entry ->
            getFieldById(entry.fieldId)?.setError(errorMessage)
        }
    } else {
        entries.each { entry ->
            getFieldById(entry.fieldId)?.clearError()
        }
    }
}

Don't forget to replace the field IDs with your own!

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events