Set Parent ID programatically

I have a requirement to provide parent ID when the jira issue is created programatically.

When I set the parent ID, my issue (sub task/sub bug) has to be created under the  given parent ID issue.

Any idea, how can I do this.

I jsut set the issue id long value to my newly creatin issue. But nothing happens.

issue.parentID(my_parent_Id's_ID); 

4 answers

0 votes
Gaston Valente Community Champion Sep 03, 2017

Sahan,

you need to use the subtaskmanager https://docs.atlassian.com/jira/server/com/atlassian/jira/config/SubTaskManager.html

there are a lot of post in the community with code sample, for example:

https://community.atlassian.com/t5/Answers-Developer-Questions/How-do-I-programmatically-convert-an-issue-to-a-subtask-of/qaq-p/553198

If you need more help I think I have a working snippet somewhere

HI Gaston,

Please share... My code not working. Tried a lot.. 

Not sure where you got "parentID" from, but it's not going to work because you might not have it until later if you are running this on "create"

Try using the issue object for the parent:

newSubTask.setParentObject(issue)

Hi Gaston,

Thanks for the quick reply. I tried subTaskManager.createSubTaskIssueLink(Issue parent, Issue subTask, User user) API. It says NoSuchMethod. 

 

And, For Nic, I jsut tried. It creates the sub task issue seperately. But it doesn't create the subtask under the parent issue. 

I do reindex after issue creation too.

Am I doing something wrong..

Thanks Gaston.

 

I managed to do your way with reflections. That worked! 

Thank you both!!!

0 votes
Gaston Valente Community Champion Sep 04, 2017

Sahan,

This code worked for me:

private void convertToSubtask(MutableIssue issue, Issue parentIssue)
{
SubTaskManager subTaskManager = ComponentAccessor.getSubTaskManager()
issue.setProjectObject(parentIssue.getProjectObject())
issue.setIssueTypeObject( subTaskManager.getSubTaskIssueType( this.defaultSubtaskId ) )
issue.setParentId(parentIssue.getId())
issue.store()

subTaskManager.createSubTaskIssueLink(parentIssue, issue, this.runAs)

//reindex
this.reindexIssue(issue)
}

Keep in mind what Nick said about the timing... 

Great Thanks Gaston. I will try that and let you know how it goes.

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,789 views 11 18
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot