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

how to validate if user is a part of a group

Arunajyothi Samala May 6, 2024

Hi

I have two groups.

First group: if a user from first group is creating the JSM ticket, no validation, go and ahead and create the issue

Second group: if the user is from second group, show a custom field with group of users in it. Make it required. then submit the  ticket.

I am new to scriptrunner. 

1. Do I have to use a Validator or should I a use scriptRunner behaviour to acheive this?

 

1 answer

0 votes
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.
May 6, 2024

Hi and welcome to the wonderful world of scriptrunner!

Let's clarify some components of Jira/Scriptrunner.

1) Workflow Validator:  whether provided by native Jira functionality, scriptrunner, or any other add-on, workflow validators execute AFTER the user clicks submit on a transition screen but before anything is saved.

2) Behaviours: can be executed any time an issue screen is displayed (creation, edit, or transition screens). This can be used to validate user entries or otherwise adjust elements on the screen based on user input. Note that fields have to be present on the screen for behaviours to do something to it. A field can be hidden (or not). But a new field can't be added.

Similarly, behaviours can make a field required when it is not otherwise set as required by the field config or the workflow. But it cannot make it optional if it is required by those other components.

So since your requirement is to "Show" a field when a user is part of group 2, what we really need then is to HIDE it when the user is NOT part of group 2. This can be accomplished with a behaviour script.

Because your requirement is not dependent on another field being modified, then the best place to put this logic is in an "initializer" script. This script runs when the screen initially opens.

Here is a sample script to get you going in your behaviour journey:

 

def conditionalCustomFieldName = 'Your custom field to show'
def groupToShowFieldTo = 'group 2'

def currentUser = Users.loggedInUser

def behaviourFormField = getFieldByName(conditionalCustomFieldName)
def showField = currentUser.isMemberOfGroup(groupToShowFieldTo)
behaviourFormField.setRequired(showField).setHidden(!showField)

This presumes that your custom field exists, is on the screen, and is not required by Jira configurations. Also, that field must already be configured to allow the correct type of input. s

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events