Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

To create a subtask with a condition

SysAdmin August 20, 2024

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.

1 answer

1 accepted

0 votes
Answer accepted
Benito Jiménez _Sevidev_
Community Champion
August 20, 2024

Hi @SysAdmin , here you have a functional script:

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.issueType
        !parentSubTasks.find{it.issueType == currentSubTaskType}
    }
}

 There were some error that I have fixed.

SysAdmin August 21, 2024

Thanks @Benito Jiménez _Sevidev_ 

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
}

 

 

Suggest an answer

Log in or Sign up to answer