Groovy Script validator for user and a field

I am trying to add one Validator into my workflow with one simple condition .

If the user = "abcd" and field = yes then create else give an error "Please fill all the Value"

and if the user !="abcd" then dont check the field just create it 

how can i achieve this in Groovy .

Your Help is really appreciated 

Abyakta

1 answer

Using ScriptRunner, you can go to the workflow of the project that you would like this validator to be present. Then, when editing the workflow, click on the diagram view and select the "Create" transition. There will be a pop-up to the right, click "Validators" in the pop-up and then select "Add Validator." Select "Script Validator" and choose a "Simple Scripted Validator."

In the condition, to do what you're looking for, you'll need to put something link this:

import com.atlassian.jira.component.ComponentAccessor

def userManager=ComponentAccessor.getUserManager()

currentUser.equals(userManager.getUserByName("Some User)) && cfValues['Some Custom Field'] == 'Some Value'


Something like this would validate for any custom field that you specify. If you're just trying to see if the field is set, change the second part of the condition to:

cfValues['Some Custom Field'] != null

I tried the below but it is not allowing to create ticket 

 

Just test the first condition.

 

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category

def userManager=ComponentAccessor.getUserManager()
currentUser.equals(userManager.getUserByName("wtuscan")) && cfValues['Some Custom Field'] == 'Yes'

Saved the validator . when user wtuscan is trying to create a ticket with "Some Custom Field" == Yes it is giving error , it should allow it .but its not . when "Some Custom Field" == No then it should give error .

So, that script will only allow the wtuscan user to pass the validator. I think you'd want something more like

import com.atlassian.jira.component.ComponentAccessor

def userManager=ComponentAccessor.getUserManager()

!currentUser.equals(userManager.getUserByName("wtuscan")) || cfValues['Some Custom Field'] == 'Yes'

That should return true any time that the user isn't wtuscan OR the user is wtuscan, but they filled in the custom field with the value of "Yes". Does that make sense?

@Jonny Carter [Adaptavist]

I tried the above code . but its not working.

What i am trying to do is 

if the custom field is not selected then it doesnt matter who is the user 

but if the user is wtuscan "and" the custom field is selected to YES then it should allow to create ticket

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,189 views 13 19
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot