How to change sub-task parentId using Groovy (Scriptrunner)

Jonathan V. June 28, 2022

I am trying to change the parentId of a sub-task via a Groovy script in the Scriptrunner Console, but for some reason the change is not actually being saved.

Below, is the code I am using.

As you will see, I am getting the new parent from the change history of the sub-task, as it was moved by mistake. We have multiple cases like this, which is why I am trying to do this via a script.

Any help or guidance is appreciated.

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.*
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils

def issueManager = ComponentAccessor.getIssueManager();
def changeHistoryManager = ComponentAccessor.changeHistoryManager
def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)

final String issueKey = 'CLTAST-18782'
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issue = ComponentAccessor.issueManager.getIssueByCurrentKey(issueKey)
def parent = ""

changeHistoryManager.getChangeItemsForField(issue, "Parent").each { def it -> parent = it.getFromString(); log.warn(it.getFromString()) }

if (issue.getParentObject().getKey() != parent){
log.warn ("Not the same parent")
def parentIssue = ComponentAccessor.issueManager.getIssueByCurrentKey(parent)
def parentId = parentIssue.getId()
issue.setParentId(parentId)
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false);

boolean wasIndexing = ImportUtils.isIndexIssues();
ImportUtils.setIndexIssues(true);
log.warn("Reindex issue ${issue.key} ${issue.id}")
issueIndexingService.reIndex(issueManager.getIssueObject(issue.id));
ImportUtils.setIndexIssues(wasIndexing);
}

 

1 answer

1 accepted

0 votes
Answer accepted
Nic Brough -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.
June 28, 2022

Sub-tasks are not linked to their containing issue by the parentID.  It's actually done with a hidden issue link type, but you don't need to worry about that directly.

Use SubTaskManager.changeParent(subtask, new parent, current user)

Jonathan V. June 28, 2022

Thanks Nic, let me give that a try.

Jonathan V. June 28, 2022

Can you share how to invoke it, and which package would need to be imported? I've actually never used this interface.

Jonathan V. June 28, 2022

I found it on another thread, and was able to implement it and it worked perfectly.

Thanks again Nic.

Nic Brough -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.
June 28, 2022

From memory:

import com.atlassian.jira.config.SubTaskManager
def subTaskManager = ComponentAccessor.SubTaskManager
 subTaskManager.changeParent(issue, parentIssue, user)
Jonathan V. July 14, 2022

Thanks Nic, I forgot to reply earlier

Deepali Kohli March 9, 2024

Hello @Nic Brough -Adaptavist- ,

I am trying to create Subtask in script console, but with below script it is creating orphan subtask (its not link to parent ticket). If it matters we are dc v.9.12.4

def parentIssue = Issues.getByKey('SR-1')

parentIssue.createSubTask('Sub-task') {
setSummary('This is a sub-task...')

}

Also once subtask created I tried to update the orphan subtask with parent issue with below code but it doesn't update it

def issueManager = ComponentAccessor.getIssueManager()
def subTaskManager = ComponentAccessor.getSubTaskManager()
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

 

def issue = Issues.getByKey('PS-4190')
def subTask = Issues.getByKey('PS-9551')

 

subTaskManager.changeParent(subTask,issue, user)

I also Tried the above script in automation rule (with scriptrunner script) I get below error

Action details:
Script function failed on Automation for Jira rule:
Script function failed on Automation for Jira rule: testing NPI Subtask, file: <inline script>, error: java.lang.NullPointerException: Cannot invoke "java.lang.Long.equals(Object)" because the return value of "com.atlassian.jira.issue.link.IssueLink.getDestinationId()" is null


Can you please guide what am I missing on updating the orphan subtask
Looking forward to hear from you,

Thanks!

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.4.0
TAGS
AUG Leaders

Atlassian Community Events