Forums

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

Validate users selected on JSD form using behaviors

Kian Stack Mumo Systems
Community Champion
December 31, 2019

This request initially began as an attempt to limit the number of users that could be selected in a multi-user picker field.

One of the service requests in JSD allowed customers to request a new shared mailbox along with the people who should have access. There is a limit of 18 people who can have access.

Looking into behaviors, I found that the function formField.getValue() does not return a list of users, but rather returns a String. How can I retrieve each user object to perform validations?

 

FYI - I am no  longer trying to limit  the number of users, we are instead warning people in the help text not to select more than 18. This question relates more to the general idea of checking the contents of a multi user picker field for whatever validations we want.

Initial code used to "Solve" the above 18 limit. Note that we are counting the number of commas as I couldn't cast the list to application users.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.user.ApplicationUsers


//Get the Owner custom field
def owners = getFieldByName("Add Users")
def ownerValue = owners.getValue() as List<String>
def count = 0

def fieldValues =
//Count the number of commas present. This will give us a number of values 1 larger than the number of commas we find.
ownerValue.each{
if(it == ","){
count++
}
}
//If there is more than 17 commas, we have more than 18 values. Set an error on the field.
if(count >17){
owners.setError("Only 18 users may have access too this mailbox, please reduce the number of users selected.")
}
else{
owners.clearError()
}

 

1 answer

1 vote
brbojorque
Community Champion
December 31, 2019

Hi @Kian Stack Mumo Systems ,

You can break the string into an array and make the comma a delimeter.

You then loop the array to get the user object.

My question is, what kind of validation you want to do in each user object?

I feel like what you have right now is optimal performance wise. Let me know if i'm missing something.

Kian Stack Mumo Systems
Community Champion
December 31, 2019

Bryan,

 

The issue with using Comma as the delimiter is if one of the usernames (unlikely) contains a comma. That would throw the count off and prevent casting into user objects. It is an edge case, but I've seen service accounts with a comma in the name before.

I can't think of a strong use case for performing other validations, I was just curios if you could get them in a way other than iterating over them with comma as the delimiter.

Thanks,

Kian

brbojorque
Community Champion
December 31, 2019

Hi @Kian Stack Mumo Systems ,

Ahh I see, getValue() is behaviour is only function

What you need is to use the Java API for retriving the value like so below, it will return a collection which you can then loop.

issue.getCustomFieldValue(ownerCF)
Kian Stack Mumo Systems
Community Champion
December 31, 2019

@brbojorque 

That call would only work if the custom field value was already stored, correct? In that case, it wouldn't be able to work on the forms being filled out as they don't store the value until creation/update.

Thanks,

Kian

brbojorque
Community Champion
December 31, 2019

Correct, my bad I'd have to do more research on this. I'm actually surprise that it is returning string rather than array.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events