Groovy Script validator for user and a field

Abyakta Lenka April 13, 2017

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

2 answers

0 votes
Nikolai Ruban April 18, 2019

JIRA Groovy Script example,  can be used with free plugin MyGroovy.
To apply script to the workflow:
1. Use  script as Inline or Registry (script should have Validation checkmark)
2. Add validator on Transition
Script check Custom Field "Epic Link" for Value, if value was not provided throws error and  blocks transition.

 


import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Epic Link")
if(cf){
 Issue epic = issue.getCustomFieldValue(cf);
 if(!epic)
 {
 InvalidInputException error= new InvalidInputException();
 error.addError("Epic Link should be set.");
 throw error;
 }
}




0 votes
Aidan Derossett _Adaptavist_
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.
April 14, 2017

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
Abyakta Lenka April 15, 2017

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 .

Jonny Carter
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.
April 21, 2017

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?

Abyakta Lenka June 5, 2017

@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 Sign up to answer