Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Block WF transition to "Done" for certain issue types based on a field

Alex T.
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
December 29, 2023

Morning all!! 

I have created a new WF and facing some issues. I need the workflow to block the transition to "Done" if the field "Checks" (It is a checkbox field) hasn't the 5 checks marked.

That's easy to do with a validator. The fact is that I need this validator / condition to only apply if the issue type is "Story" or "Task" so the transition is not being blocked for subtasks or any other issue types.

Ideally, would be great if when blocking the transition a error message appears saying "This transition is not allowed until field "Checks" is completed".

At the moment I have tried several scripts with Script Runner using both validators and conditions (see below).

 

Custom Script Condition:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.opensymphony.workflow.InvalidInputException

def issueType = issue.issueType?.name
def allowedIssueTypes = ["Story", "Task"]

if (allowedIssueTypes.contains(issueType)) {
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def checkBoxField = customFieldManager.getCustomFieldObjectsByName("Checks").find { it.name == "Checks" }

def checkBoxValues = issue.getCustomFieldValue(checkBoxField) as List<Boolean>
def requiredChecks = 5

if (!checkBoxValues || checkBoxValues.count { it == true } < requiredChecks) {
throw new InvalidInputException("This transition is not allowed until field "Checks" is completed")
}
}

return true

 

Custom Script Validator:

 

import com.atlassian.jira.component.ComponentAccessor

def issue = issue
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def checksFieldName = "Checks"
def checksField = customFieldManager.getCustomFieldObjects(issue).find { it.name == checksFieldName }

if (issue.issueTypeObject.name in ["Story", "Task"]) {
def checksValue = issue.getCustomFieldValue(checksField)
if (checksValue?.size() == 5 && checksValue.every { it }) {
return true
} else {
def errorMessage = "This transition is not allowed until field "Checks" is completed"
log.error(errorMessage)
return false
}
} else {
return true
}

 

Non of them have errors but don't work... The Stories are still being transitioned to "Done" without any error message (The field Checks is already included in the tasks and empty, the error should appear).

Thank you in advance!

1 answer

0 votes
Nic Brough -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 Champions.
December 29, 2023

I think 

if (checksValue?.size() == 5 && checksValue.every { it })

is the problem.  When "Checks" has no values at all, the field will be null, so checksValue? will return "true" and the checksValue will also always return true, so the "if" is always going to come out as "true".

I would drop the checksValue.every part of it, as that's of no use at all, and change the if to be "if checksValue is empty or has a size lower than 5"

Suggest an answer

Log in or Sign up to answer