To create a subtask with a condition
Asked by SysAdmin:

Hi,

Trying to create a subtask using create a subtask postfunction by script runner in jira with a condition.

**Condition**: If a single select custom field is 'Yes' and same subtask type is not created yet then create a subtask

Was writing a condition for the same. But it is not working, any thoughts?

//cfValues\['Is new hardware RQD?'\]?.value == 'Yes' \&\&

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.customfields.option.Option

import com.atlassian.jira.issue.fields.CustomField

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

CustomField ishwrqd = customFieldManager.getCustomFieldObject("customfield_11604")

Option customFieldValue = (Option) issue.getCustomFieldValue(ishwrqd)

def customFieldValueRaw = customFieldValue.getValue()

if (customFieldValueRaw.equals("Yes"){

if(issue.isSubTask())

{

def issueParent = issue.getParentObject()

def parentSubTasks = issueParent.getSubTaskObjects()

def currentSubTaskType = issue.getissueType()

!parentSubTasks.find{it.getType() == currentSubTaskType}

}

}

Regards.


Answers:
author: Benito Jim?nez _Sevidev_
body: Hi [@SysAdmin](/t5/user/viewprofilepage/user-id/3609486) , here you have a functional script:

```
import com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.issue.CustomFieldManagerimport com.atlassian.jira.issue.customfields.option.Optionimport com.atlassian.jira.issue.fields.CustomFieldCustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()CustomField ishwrqd = customFieldManager.getCustomFieldObject("customfield_11604")Option customFieldValue = (Option) issue.getCustomFieldValue(ishwrqd)def customFieldValueRaw = customFieldValue.getValue()if (customFieldValueRaw.equals("Yes")) {    if(issue.isSubTask()) {        def issueParent = issue.getParentObject()        def parentSubTasks = issueParent.getSubTaskObjects()        def currentSubTaskType = issue.issueType        !parentSubTasks.find{it.issueType == currentSubTaskType}    }}
```

There were some error that I have fixed.

comments: author: SysAdmin
body: Thanks [@Benito Jim?nez _Sevidev_](/t5/user/viewprofilepage/user-id/5571291)

For the correction.

But as per the use case I had to script it as below which is working

```
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
//def issue = Issues.getByKey('ABC-000')

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField ishwrqd = customFieldManager.getCustomFieldObject("customfield_Id")
Option customFieldValue = (Option) issue.getCustomFieldValue(ishwrqd)
def customFieldValueRaw = customFieldValue.getValue() as String
def found = true

if(customFieldValueRaw == "Yes"){

def sibling = issue.getSubTaskObjects()
if(sibling.size()>0){
for(Issue child:sibling){
log.warn "child"+child.getIssueType().getName()+"###"
if(child.getIssueType().getName()== "subtask name"){
found = false
}
}
} else if(sibling.size()==0){
return true
}
return found; 
}
else{
return false
}
```


Tags: data-center

jira-data-center

jira-software