Problem creating subtask within a script

Hi all!

 

I also posted this on stackoverflow, but did not get any answer. Hopefully someone here can help me with my problem. I had this script running, but tried to remove deprecated functions. I removed almost all of them, but I am stuck on this code.

 

I am working on some groovy-scripts in jira. The script get´s triggered as a postfunction using the scriptrunner plugin. The code, that produces the error is this one:

ComponentAccessor CompAcc = new ComponentAccessor() 
SubTaskManager subTaskManager = CompAcc.getSubTaskManager() 
... 
def subTask = issueManager.createIssue(CompAcc.getJiraAuthenticationContext().getLoggedInUser(), issueObject) 
subTaskManager.createSubTaskIssueLink(issue, subTask, usera)

where issue is the issue, on which the postfunction was triggered. The subtask get´s created but not linked to the calling task.

And this is the error:

2016-01-11 11:39:39,925 http-bio-8063-exec-19 ERROR asu 699x3972x1 16m4yz4 10.1.21.129 /secure/CommentAssignIssue.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] ************************************************************************************* 
2016-01-11 11:39:39,926 http-bio-8063-exec-19 ERROR asu 699x3972x1 16m4yz4 10.1.21.129 /secure/CommentAssignIssue.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: RQA-7338, actionId: 81, file: /home/qa/Tools/Jira/scripts/CreateDevTestExecutionSubtask.groovy groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.config.DefaultSubTaskManager.createSubTaskIssueLink() is applicable for argument types: (com.atlassian.jira.issue.IssueImpl, com.atlassian.jira.ofbiz.IssueGenericValue, com.atlassian.jira.user.DelegatingApplicationUser) values: [RQA-7338, [timespent:null, timeoriginalestimate:7200, project:11400, ...], ...] Possible solutions: createSubTaskIssueLink(com.atlassian.jira.issue.Issue, com.atlassian.jira.issue.Issue, com.atlassian.crowd.embedded.api.User), createSubTaskIssueLink(org.ofbiz.core.entity.GenericValue, org.ofbiz.core.entity.GenericValue, com.atlassian.crowd.embedded.api.User) at CreateDevTestExecutionSubtask.run(CreateDevTestExecutionSubtask.groovy:212)

 

So what I need to know, is how I can convert com.atlassian.jira.issue.IssueImpl andcom.atlassian.jira.ofbiz.IssueGenericValue to com.atlassian.jira.issue.Issue so that the linking of the subtask works again. Or someone can help me to explain, how I have to use the missing function.

Thanks for any help!

2 answers

1 accepted

Hi Andreas

An example on how to create subtask.

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParametersImpl

def issueService = ComponentAccessor.getIssueService();
def issueInputParameters = new IssueInputParametersImpl();
def applicationUser = ComponentAccessor.getJiraAuthenticationContext().getUser()
def subTaskManager = ComponentAccessor.getSubTaskManager()

Issue parentIssue = issue
issueInputParameters.setProjectId(parentIssue.getProjectId()).setIssueTypeId('5')
        .setSummary('A new sub task').setDescription('sub task description')

def parentIssueId = parentIssue.id
IssueService.CreateValidationResult createValidationResult =
        issueService.validateSubTaskCreate(applicationUser, parentIssueId, issueInputParameters);
if (createValidationResult.isValid()) {
    IssueService.IssueResult createResult = issueService.create(applicationUser, createValidationResult);
    subTaskManager.createSubTaskIssueLink(parentIssue, createResult.getIssue(), applicationUser);
}

This is for JIRA versions => 7. For JIRA versions < 7 convert application user to user:   

def applicationUser = ComponentAccessor.getJiraAuthenticationContext().getUser().directoryuser

Please let me know if this works for you

The setIssueTypeId('5') is the id of the subTask issue type

Yes! Thanks so much! I had to change all my code, but it works now. Only one thing: I am trying to set fixversions and affected versions from the parent. But I seem to miss something. This crashes:

issueInputParameters.setFixVersionIds(parentIssue.getFixVersions()) 
issueInputParameters.setAffectedVersionIds(parentIssue.getAffectedVersionIds()) 

 

it ends up in this:

2016-01-12 16:44:03,724 http-bio-8063-exec-25 ERROR asu 1004x23301x1 13hl2hm 10.1.21.129 /secure/CommentAssignIssue.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] *************************************************************************************
2016-01-12 16:44:03,724 http-bio-8063-exec-25 ERROR asu 1004x23301x1 13hl2hm 10.1.21.129 /secure/CommentAssignIssue.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: RQA-7338, actionId: 81, file: /home/qa/Tools/Jira/scripts/CreateDevTestExecutionSubtask.groovy
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueInputParametersImpl.setFixVersionIds() is applicable for argument types: (java.util.ArrayList) values: [[16.0.1]]
Possible solutions: setFixVersionIds([Ljava.lang.Long;), getFixVersionIds()
at CreateDevTestExecutionSubtask.run(CreateDevTestExecutionSubtask.groovy:105)

 

The parentIssue.getFixVersions() will return to you a collection of versions but you want the ids, therefore try def listOfVersions = parentIssue.getFixVersions()?.collect {it.id} the same applies for the affectedVersions

Thx for the hint! I am totally new to groovy and sometimes, I don´t see the whole picture! So what I did was the following: I created the listOfVersions as you sugested, but it gave me the wrong type. So I used parentIssue.getFixVersions()?.collect {it.getId()} this gives me objects of type long. But when I tried to set the fixversion of the subtask, I got an error, saying, that it can´t cast from arraylist to long. It works, if I specify one element by using [index]. But what should I do, if I have more than one fixversions in the parent issue? I tried this: def listOfFixVersions = parentIssue.getFixVersions()?.collect {it.getId()} for (curvers in listOfFixVersions) { println("adding version " + curvers) issueInputParameters.setFixVersionIds(curvers) } But this only sets the last value, of course. So my question is, how should I use my array in this special case. And what might be more important: Where can I get this knowledge, so that I don´t have to bother others.

Andrea Try to assign the versions after the subtask is created, in your case: if (createValidationResult.isValid()) { IssueService.IssueResult createResult = issueService.create(applicationUser, createValidationResult) subTaskManager.createSubTaskIssueLink(parentIssue, createResult.getIssue(), applicationUser) createResult.getIssue().setFixVersions(parentIssue.getFixVersions()) } With the setFixVersions you need the versions and not the ids therefore you don't need to 'collect' them Please let me know if this work for you

Yes! Thanks again for the hint! I spent hours on that script! It works now! All suggestions you gave are perfectly working!

Glad to help, regarding your question "where you can get this knowledge" first I would suggest the SR documentation https://scriptrunner.adaptavist.com/latest/jira/quickstart.html. Also in Atlassian Answers there is always a good chance to find an already answered question which can be similar to yours and especially for SR lot of users share their (very often great) scripts / solutions. And finally if nothing of the above works you can post your question and I am sure that someone will give you a hint or even the solution (I do not think that anyone feels 'bothered' :) )

Suggest an answer

Log in or Join to answer
Community showcase
Emilee Spencer
Published yesterday in Marketplace Apps

Marketplace Spotlight: DeepAffects

Hello Atlassian Community! My name is Emilee, and I’m a Product Marketing Manager for the Marketplace team. Starting with this post, I'm kicking off a monthly series of Spotlights to highlight Ma...

46 views 0 3
Read article

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