Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Creating a sub-tsk via scriptrunner gives an error even using the example given.

Lori Blatt
April 27, 2023
Here is the code in scriptrunner:

// Specify the key of the parent issue here
def parentKey = 'FN-29722'

// Get the parent issue type
def issueResp = get("/rest/api/2/issue/${parentKey}")
.asObject(Map)
assert issueResp.status == 200

// get the body of the parent issue type
def issue = issueResp.body as Map

// Get the issue types for the instance
def typeResp = get('/rest/api/2/issuetype')
.asObject(List)
assert typeResp.status == 200
def issueTypes = typeResp.body as List<Map>


// Here we set the basic subtask issue details
def summary = "DEV::BATCH - test" // The summary to use for
def issueType = "Sub-task" // The Sub Task Issue Type to Use

// Get the sub task issue type to use
def issueTypeId = issueTypes.find { it.subtask && it.name == issueType }?.id
assert issueTypeId : "No subtasks issue type found called '${issueType}'"

// Get the project to create the subtask in
def project = (issue.fields as Map).project

// Create the subtask
def resp = post("/rest/api/2/issue")
.header("Content-Type", "application/json")
.body(
fields: [
project: project,
issuetype: [
id: issueTypeId
],
parent: [
id: issue.id
],
summary: summary
])
.asObject(Map)

// Get and validate the newly created subtask
def subtask = resp.body
assert resp.status >= 200 && resp.status < 300 && subtask && subtask.key != null

// If the sub task created successfully return a success message along with its key
if (resp.status == 201) {
return 'Success - Sub Task Created with the key of ' + resp.body.key.toString()
} else {
return "${resp.status}: ${resp.body}"
}

 

 

Here is the error:

 

Serializing object into 'interface java.util.Map'
GET /rest/api/2/issue/FN-29722 asObject Request Duration: 659ms
Serializing object into 'interface java.util.List'
GET /rest/api/2/issuetype asObject Request Duration: 316ms
Serializing object into 'interface java.util.Map'
POST /rest/api/2/issue asObject Request Duration: 564ms
POST request to /rest/api/2/issue returned an error code: status: 400 - Bad Request
body: {errorMessages=[], errors={issuetype=The issue type selected is invalid., project=Issues with this Issue Type must be created in the same project as the parent.}}
Assertion failed: 

assert resp.status >= 200 && resp.status < 300 && subtask && subtask.key != null
       |    |      |      |  |    |      |     |          |
       |    400    true   |  |    400    false false      false
       |                  |  status: 400 - Bad Request
       |                  |  body: {errorMessages=[], errors={issuetype=The issue type selected is invalid., project=Issues with this Issue Type must be created in the same project as the parent.}}
       |                  false
       status: 400 - Bad Request
       body: {errorMessages=[], errors={issuetype=The issue type selected is invalid., project=Issues with this Issue Type must be created in the same project as the parent.}}

Class: com.adaptavist.sr.cloud.events.ConsoleScriptExecution, Config: [userTriggered:true]

 

1 answer

Suggest an answer

Log in or Sign up to answer
2 votes
Daniel Yelamos [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 Champions.
September 18, 2017

Hello Kevin.

Yes this is entirely possible using a postfunction.

In the simplest case, let's say that you want to copy the original issue to the Epic and then to the task.

You would need to:

-----------------------

1. Go to your workflow.

2. Edit your workflow.

3. Go to the transition that you want this to trigger on:

4. Add Scriptrunner postfunction.

5. Select Copy Issue and Links postfunction.

6. Select the fields that you want to propagate to the next issue, for example

     a) Description

     b) Version number

7. Select the issue type as an Epic.

8. Save the postfunction and move it to the last place in the list of postfunctions in your transition.

-----------------------

Repeat this process for your other 

(task) Desgin Product X

(task) Build Product X 

(task) Test Procuts X

BUT bear in mind to set the issue type as "Subtask" or "Task"

 

In the end you would have a total of 4 postfunctions. The first one would be for your epic, and the rest for your design, build and test steps.

Please do say if I can be of anymore help.

Cheers!

Dyelamos

Kevin Seery
Contributor
September 19, 2017

Hi Daniel,

 

This is a great soltuon.

Thanks for your help on this 

Kevin

Daniel Yelamos [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 Champions.
September 19, 2017

No problem. Could you please accept my answer so that other users know that this has been correctly answered?

Thanks!

DYelamos

sanjay.rakh
September 20, 2022

@Daniel Yelamos [Adaptavist]  I don't see Select Copy Issue and Links within workflow postfunction.

TAGS
AUG Leaders

Atlassian Community Events