ScriptRunner Checking sibling subtask Edited

I am trying to use the "Checking sibling subtasks" example (https://scriptrunner.adaptavist.com/latest/jira/recipes/workflow/validators/simple-scripted-validators.html#_checking_sibling_subtasks)

import com.atlassian.jira.issue.customfields.option.Option 
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("SelectListA")
if (! issue.isSubTask()) {
return true
}
def parent = issue.parentObject
def selectedValue = issue.getCustomFieldValue(cf) == "Some Value" ! parent.subTaskObjects.any { subtask -> subtask.getCustomFieldValue(cf) == selectedValue }

 The only part I changed was: def selectedValue = issue.getCustomFieldValue(cf) as Option to become:

def selectedValue = issue.getCustomFieldValue(cf)== "Some Value"

I swear this worked earlier today, and I was just finessing some post functions, but now this condition is not working i.e. it keeps recreating sub-tasks even though a sibling sub-task has the custom field value already set

1 answer

Hi Kate,

Could you explain your use case a bit more? What are you trying to accomplish? Why did you make that change to the script on the website?

It looks like you are trying to check all sub-tasks to ensure that they do not have a particular value set. Is that correct? The example on the website only checks to make sure that sub-tasks don't have the exact same value as the parent. It doesn't check for whether or not the sub-task custom field value is set.

Regards,

Josh

It looks like you are trying to check all sub-tasks to ensure that they do not have a particular value set. Is that correct?

 

Yes, that is correct.

Hi Kate,

Thanks for the information. I have tested the following script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.Option
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("SelectListA")
if (! issue.isSubTask()) {
return true
}

def parent = issue.parentObject
! parent.subTaskObjects.any { subtask ->
subtask.getCustomFieldValue(cf) as String == "AAA"
}

I have added this is as a Simple Scripted Validator to one of my transition steps. If someone tries to transition any sub-task, the script will search all sub-tasks of the parent to ensure that no one has selected "AAA" for the SelectListA value. You'll obviously need to change the script to match the name of your select list and the custom field value you want to check against.

See attached image of error message.

Screen Shot 2017-07-27 at 12.09.48 PM.png

So, I am actually am trying to do this as a Script Listener and use the code as the condition. This did not seem to honor the code you provided :(

Hey Kate,

Which version of JIRA and ScriptRunner are you using? Also, if you are using this as a Script Listener, you need the event.issue:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.Option
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issue = event.issue
def cf = customFieldManager.getCustomFieldObjectByName("SelectListA")
if (! issue.isSubTask()) {
return true
}

def parent = issue.parentObject
! parent.subTaskObjects.any { subtask ->
subtask.getCustomFieldValue(cf) as String == "AAA"
}

Could you provide a screenshot of the Script Listener page that shows all of your listener settings etc?

I am on JIRA 7.2.7 and ScriptRunner 4.3.19

I am trying to create the initial sub-task based on a checkbox value, and not re-create those sub-tasks after 1 already exists

Listener.png

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

284 views 0 6
Read article

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