Use validator to prevent creating duplicate sub-task

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

1 answer

1 accepted

Accepted Answer
1 vote
Alexey Matveev Community Champion Jan 09, 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

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 Community Champion Jan 09, 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

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

Alexey Matveev Community Champion Jan 09, 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.

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 Community Champion Jan 09, 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
}

Thank you so much!

Now it works as expected.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

21,216 views 2 7
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