Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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]

 

2 answers

2 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Kristian Walker _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.
April 28, 2023

Hi Lori,

My name is Kristian, and I am a developer from the ScriptRunner for Jira Cloud team.

Thank you for raising this issue, I have looked into this and can confirm this issue is caused by the line of your script of.

def issueTypeId = issueTypes.find { it.subtask && it.name == issueType }?.id

The reason this generates the error you are seeing with the issue type is caused by when a Team Managed project is created in an instance.

With team-managed projects they add a new context onto each issue type which means they have a different ID and the script does not then return the ID for the Global subtask type as it requires.

In order to fix this you should update this line to be as per below.

def issueTypeId = issueTypes.find { it.subtask && it.name == issueType && !it.scope }?.id

Can you please make this change and confirm if it resolves your issue?

I can also confirm, that we will be looking to get the in-app example updated so that it contains this change to fix this issue.

 

Regards,

Kristian

Lori Blatt April 30, 2023

Thank you so much for the help.  The explanation was perfect and it worked.

0 votes
Answer accepted
Jeroen Poismans
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 27, 2023

Hi Lori,

In the call to create the subtask I see you are passing the project as an object. The Jira Cloud REST API v2 however states that an id should be passed:

"project": { 
"id": "10000"
}

Can you try this (with your project id of course :-))?
Here is a link to the API:
https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-issues/#api-rest-api-2-issue-post

 

Jeroen

Lori Blatt April 27, 2023

How can I get the projectId from the project object?  

Note - this was working up until yesterday afternoon.

Lori Blatt April 27, 2023

I am trying this statement but I get an error (Cannot find matching method) on it:

def projectId = issue.getProjectObject().getID()

Lori Blatt April 27, 2023

If I change the code to do this where projectKye = "FN":

//project: project,
"project":["key": projectKey],

Then I get this error:

The issue type selected is invalid., project:Issues with this Issue Type must be created in the same project as the parent.

Jeroen Poismans
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 27, 2023

Hi Lori,

Try debugging it one step at a time. First try with hard-coded values, this worked for me:

{
"project": {
"id": "10001"
},
"issuetype": {
"id": "10006"
},
"summary": "Testing",
"parent": {
"id": "10098"
}
}

Replacing with the values for your projectId, issuetypeId and parentId (of the parent issue). Then we go from there replacing with actual dynamic content.

 

Jeroen

Jeroen Poismans
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 27, 2023

In your code the above would be like this:


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

Give it a try!

 

Jeroen

Lori Blatt April 27, 2023

Note - this was working in production yesterday morning and then stopped some time yesterday afternoon.  So, this morning I went back to scriptrunner sample for Create Sub-task and that does not work in our cloud instance either?

No luck:

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

Error:
The issue type selected is invalid., project=Issues with this Issue Type must be created in the same project as the parent.

Lori Blatt April 27, 2023

So I had the wrong issuetype id - I changed it and it worked:

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

Jeroen Poismans
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 27, 2023

Yeah, that's what I was thinking. Now the challenge will be changing these hardcoded values 1 by 1 and see where it goes wrong or hopefully ... it won't 😃

Jeroen

Lori Blatt April 27, 2023

I took my original code and hard coded the issueTypeId and it works so somehow this code is getting an id of 10031 instead of 10003?

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

Jeroen Poismans
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 27, 2023

Here 's what you can do to see what issuetype is really retrieved with the code. Change this url for your site and then put id=10031 at the end, it will show you the issuetype:

https://<your-site>.atlassian.net/secure/admin/EditIssueType!default.jspa?id=10000
Lori Blatt April 27, 2023

Thank you soooo much for helping.

When I put in the 10031 I get a 500 error but 10003 brings back the correct subtask

Lori Blatt April 27, 2023

FYI - I am on PTO tomorrow.  I did put in a fix that allows our production system to run but I would like to figure out why the incorrect id is getting pulled back from the above code.

Jeroen Poismans
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 27, 2023

Yeah, that's weird indeed. Probably because spelling, something wrong in the find-issuetype code ... some log statements and interupts at places in the code will get you there.

Please accept the answer if it helped you.

Jeroen

TAGS
AUG Leaders

Atlassian Community Events