It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

2 answers

1 accepted

1 vote
Answer accepted

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

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.

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

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.

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? 

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

375 views 1 3
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you