Use validator to prevent creating duplicate sub-task

Mobileye Atlassian Licenses January 9, 2018

Hello,

I want to use validator of a transition to prevent creating the same sub-task with the same summary.

 

For that - I'm using the following simple scripted validator:

ScriptRunner workflow function - Simple scripted validator (condition apply).

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager

def issueManager = ComponentAccessor.getIssueManager()

Collection subTasks = issue.getSubTaskObjects()

if(issue.isSubTask())
{
    def issueParent = issue.getParentObject()
    def parentSubTasks = issueParent.getSubTaskObjects()
    
    def currentSubTaskSummary = issue.getSummary()
    
    !parentSubTasks.find{it.getSummary() == currentSubTaskSummary}
}

 

The problem is that I still see the option to use the transition even though I'm not expecting to see it with this validator.

 

Any idea? 

 

Thanks,

Bar

2 answers

1 accepted

1 vote
Answer accepted
Alexey Matveev
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.
January 9, 2018

Hello,

Validator do not hide transition buttons. They just check the input in the transition screen and can throw an error if the values are wrong. 

What you are looking for are conditions not validators

Mobileye Atlassian Licenses January 9, 2018

Hi Alexey,

Thanks for your answer.

You are right! I got confused, but when I'm trying to use this script in the Conditions it still let me use the transition.

Do you know why?

 

Thanks,

Bar

Alexey Matveev
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.
January 9, 2018

Condition must return true or false. Your script does not return anything. Try like this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager

def issueManager = ComponentAccessor.getIssueManager()

Collection subTasks = issue.getSubTaskObjects()

if(issue.isSubTask())
{
def issueParent = issue.getParentObject()
def parentSubTasks = issueParent.getSubTaskObjects()

def currentSubTaskSummary = issue.getSummary()

if(parentSubTasks.find{it.getSummary() == currentSubTaskSummary}) {
return false
} else {
return true
}


}
return false
Mobileye Atlassian Licenses January 9, 2018

Thank you, but it still let me use the transition.

Alexey Matveev
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.
January 9, 2018

The transition button will be hidden only if your script returns false. Try to put log.error in your code before all return statements and see if your script returns false.

Mobileye Atlassian Licenses January 9, 2018

I wrote this code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import org.apache.log4j.Logger
import org.apache.log4j.Category

def Category log = Category.getInstance("com.onresolve.jira.groovy.Condition")
log.setLevel(org.apache.log4j.Level.DEBUG)

def issueManager = ComponentAccessor.getIssueManager()

Collection subTasks = issue.getSubTaskObjects()
log.info("Sub Task ID List: " + subTasks)


def flag = 0


for (int i=0; i<subTasks.size(); i++) {
def value = subTasks[i] as String
def issueSubTask = issueManager.getIssueObject(value)

if(issueSubTask.getSummary().contains("request")){
log.info("There is a Bundle branch request sub-task")
flag = 1
return false
}
}

log.info(flag)
if(flag==0){
return true
}

 

Even if I checked that the flag equal to 1 I still can click on this transition.

 

Do you know why?

 

Thanks,

Bar

Alexey Matveev
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.
January 9, 2018

Sorry for my mistake. If you want the transition button to be hidden , you should set passesCondition variable to false.

passesCondition = false

And your script should be like this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import org.apache.log4j.Logger
import org.apache.log4j.Category

def Category log = Category.getInstance("com.onresolve.jira.groovy.Condition")
log.setLevel(org.apache.log4j.Level.DEBUG)

def issueManager = ComponentAccessor.getIssueManager()

Collection subTasks = issue.getSubTaskObjects()
log.info("Sub Task ID List: " + subTasks)


def flag = 0


for (int i=0; i<subTasks.size(); i++) {
def value = subTasks[i] as String
def issueSubTask = issueManager.getIssueObject(value)

if(issueSubTask.getSummary().contains("request")){
log.info("There is a Bundle branch request sub-task")
flag = 1
passesCondition = false
}
}

log.info(flag)
if(flag==0){
passesCondition = true
}

Mobileye Atlassian Licenses January 9, 2018

Thank you so much!

Now it works as expected.

0 votes
Ryan Bullock September 9, 2019

I have a similar request. I tried the first script but it did not work for me. 

I have a post-function that creates a subtask on transition. However, it is possible to go back in the workflow and then complete the transition again, which creates a duplicate sub-task. How can I prevent a duplicate sub-task from creating if the transition/sub-task creation had already previously occurred? 

Ryan Bullock September 9, 2019

@Alexey Matveev Any suggestions?

Suggest an answer

Log in or Sign up to answer