You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
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]
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
How can I get the projectId from the project object?
Note - this was working up until yesterday afternoon.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am trying this statement but I get an error (Cannot find matching method) on it:
def projectId = issue.getProjectObject().getID()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you soooo much for helping.
When I put in the 10031 I get a 500 error but 10003 brings back the correct subtask
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.