Conditional validation in JIRA before closing an issue

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

This widget could not be displayed.

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

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.")	
}

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")) ?: ""

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
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

112 views 2 0
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