Set Parent ID programatically

sahan kanuwana September 3, 2017

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
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.
September 4, 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... 

sahan kanuwana September 5, 2017

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

0 votes
sahan kanuwana September 3, 2017

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..

sahan kanuwana September 3, 2017

Thanks Gaston.

 

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

Thank you both!!!

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 3, 2017

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)

0 votes
Gaston Valente
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.
September 3, 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

sahan kanuwana September 3, 2017

HI Gaston,

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

Suggest an answer

Log in or Sign up to answer