Problem creating subtask within a script

Andreas Sumper January 12, 2016

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!

3 answers

1 accepted

5 votes
Answer accepted
Thanos Batagiannis [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.
January 12, 2016

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

Thanos Batagiannis [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.
January 12, 2016

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

1 vote
Andreas Sumper January 12, 2016

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)

 

Thanos Batagiannis [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.
January 12, 2016

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

Andreas Sumper January 12, 2016

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.

Thanos Batagiannis [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.
January 12, 2016

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

Andreas Sumper January 13, 2016

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

Thanos Batagiannis [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.
January 13, 2016

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' :) )

0 votes
Ramakrishnan Srinivasan
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.
March 18, 2020

Hi, 

 When I googled for jira script runner create issue "setAffectedVersionIds"

I get this link first. Thought it might help someone to add AffectedVersions.

The following lines helped me

//oIssue is the original issue from which I am creating a new issue

def affectedVersionIds = oIssue.getAffectedVersions()?.collect {it.id}
def Long_affectedVersionIds=[]
affectedVersionIds.each { it ->
    Long_affectedVersionIds.add(it as long);
}

.....

issueInputParameters.setAffectedVersionIds(Long_affectedVersionIds as Long[])

 

I achieved it with some trial and error, if there is a better way, willing to learn

 

regards

ramki

Suggest an answer

Log in or Sign up to answer