ScriptRunner Checking sibling subtask

Kate McAnespie July 25, 2017

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

0 votes
Joshua Yamdogo @ 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 Leaders.
July 27, 2017

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

Kate McAnespie July 27, 2017

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.

Joshua Yamdogo @ 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 Leaders.
July 27, 2017

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

Kate McAnespie July 28, 2017

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 :(

Joshua Yamdogo @ 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 Leaders.
July 31, 2017

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?

Kate McAnespie August 1, 2017

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
TAGS
AUG Leaders

Atlassian Community Events