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
1 vote
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 Champions.
September 15, 2017

Hi Flan,

If you want the result in a scripted field then the script will br something like 

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def months = 3
def dateACF = customFieldManager.getCustomFieldObjectByName("First DateTime")
def dateAValue = issue.getCustomFieldValue(dateACF) as Date

def dateB = Calendar.getInstance()

dateB.setTime(dateAValue)
dateB.add(Calendar.MONTH, -months)
dateB.getTime()

with

Searcher: Date Time Range picker
Template: Date Time Picker

TAGS
AUG Leaders

Atlassian Community Events