Conditional validation in JIRA before closing an issue

Karthik Bhat December 25, 2012

I have added 2 custom fields in JIRA namely

1. 'Link to Knowledge Base Requied?' -> This is a Yes/No/Not-sure dropdown box

2. 'Link to Knowledge Base' -> this is an url field where the user will paste a url

Requirement: I need to ensure that the second field i.e 'Link to Knowledge Base' is NOT blank when the value of first field i.e 'Link to Knowledge Base Requied?' is selected as 'Yes', before closing the issue. However, when either 'No' or 'Not Sure' is selected in the dropdown box, the issue can be closed regardless of the value in custom field.

I checked your documentation in https://confluence.atlassian.com/display/JIRA/Configuring+Workflow#ConfiguringWorkflow-Addingavalidator. But this does not serve my purpose. I can check a field's value before closing the issue. But what I need here is to Close the issue only if the first dropdown box's value is selected as 'Yes'. How can I do that? Any thoughts?

1 answer

1 accepted

1 vote
Answer accepted
Jobin Kuruvilla [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.
December 25, 2012

You will have to write a custom validator in this case. Either write a custom plugin or use the script runner plugin and write some simple groovy script to handle this.

In the latter case, you will find some examples here: https://studio.plugins.atlassian.com/wiki/display/GRV/Validators

Karthik Bhat December 25, 2012

Hi Jobin,

I wrote a simple groovy script as you suggested. But it doesn't seem to work. I am able to close the issue without providing any url in 'Link to Knowledge Base' field. Anything wrong with my script?

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException
 
def componentManager = ComponentManager.instance
def optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)
def customFieldManager = componentManager.getCustomFieldManager()
 
def cf = customFieldManager.getCustomFieldObjectByName("Link to Knowledge Base Required?")
def cf_value = issue.getCustomFieldValue(cf) 

def cf_1 = customFieldManager.getCustomFieldObjectByName("Link to Knowledge Base")
def cf_value_1 = issue.getCustomFieldValue(cf_1)

if ((cf_value == "Yes") && (cf_value_1 == "")) {
    invalidInputException = new InvalidInputException("Please provide the Knowledge Base link before closing the issue.")	
}

Jobin Kuruvilla [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.
December 25, 2012

You are getting it from the saved issue object, which might be null. Try similar to this example (in the link I gave above):

String sponsor = (String) WorkflowUtils.getFieldValueFromIssue(issue, WorkflowUtils.getFieldFromKey("customfield_10170")) ?: ""
Karthik Bhat December 25, 2012

I read somewhere that WorkflowUtils is not a recommended approach.

In my code, I printed the contents of both variables and found that the value of cf_value to be 'Yes' and that of cf_value_1 to be 'null'.

I was able to compare the null variable correctly by using 'if (!cf_value_1)'. But comparing the Groovy strings was a challenge. I did it finally using .toString() function like this:

if(cf_value.toString() == temp.toString())

I created a new gstring called temp and stored its value as "Yes". It may not be the best way to do it. But it worked!

Thanks a lot Jobin. You rock!

If someone needs the updated groovy script, here it is:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException
 
def componentManager = ComponentManager.instance
def optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)
def customFieldManager = componentManager.getCustomFieldManager()
 
def cf = customFieldManager.getCustomFieldObjectByName("Link to Knowledge Base Required?")
def cf_value = issue.getCustomFieldValue(cf) 

def cf_1 = customFieldManager.getCustomFieldObjectByName("Link to Knowledge Base")
def cf_value_1 = issue.getCustomFieldValue(cf_1)

def temp = "Yes"

if (!cf_value_1) {
    if(cf_value.toString() == temp.toString()) {
      invalidInputException = new InvalidInputException("Please provide the Knowledge Base link before closing the issue.")	
	}  
}

Suggest an answer

Log in or Sign up to answer