How to add subtasks to an existing issue through Jira REST API?

KnowCode Company
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 18, 2024

Hi there!

I'm trying to add a subtask to an existing issue on my development environment. But I'm not succeeding on it.

Here is my function:

const createSubtasks = async () => {
    const parentIssueKey = context.extension.issue.key;  // Use issue key instead of ID
    const projectId = context.extension.project.id;  // Continue using project ID
    const projectKey = context.extension.project.key;
    const issueType = await getID(projectId);  // Get the subtask issue type ID

    if (!issueType) {
      console.error("No valid subtask issue type found.");
      return;
    }
 
    const requestBody = {
      "fields": {
        "project": {
          "key": projectKey
        },
        "parent": {
          "key": parentIssueKey  
        },
        "summary": "Subtask Example",
        "description": {
          "type": "doc",
          "version": 1,
          "content": [{
            "type": "paragraph",
            "content": [{
              "text": "This is a detailed description of the subtask.",
              "type": "text"
            }]
          }]
        },
        "issuetype": {
          "id": issueType  
        }
      }
    };
 
    const response = await requestJira("/rest/api/3/issue", {  
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify(requestBody)
    });
 
    if (response.status === 201) {
      console.log("Subtask created successfully!");
    } else {
      console.error("Failed to create subtask:", await response.text());
    }
  };

And I'm getting this error message: Failed to create subtask: {"errorMessages":[],"errors":{"parentId":"Given parent issue does not belong to appropriate hierarchy."}}


And I don't know what else to do because I'm getting the actual key and id of the issue that I want to work on. And I can add subtasks to this issue with no problem by using the Jira software.

Can someone help me? 

Thanks!

1 answer

0 votes
waqas Wilan2026 April 18, 2024

To add subtasks to an existing issue through Jira's REST API, you'll need to send a POST request to the Jira server with the necessary information. Here's a general outline of the steps involved:

1. **Authenticate**: Make sure you have the necessary authentication credentials to access the Jira API. This typically involves using either basic authentication with a username and password or OAuth tokens.

2. **Retrieve Issue Details**: First, you'll need to retrieve the details of the existing parent issue using the Jira REST API. You'll need the issue ID or key of the parent issue to proceed.

3. **Create Subtasks**: Next, construct a JSON payload containing the details of the subtasks you want to create. This payload should include the summary, description, and any other relevant fields for each subtask.

4. **Send POST Request**: Use the Jira REST API endpoint to create subtasks under the parent issue. The endpoint typically looks like `/rest/api/2/issue/{issueIdOrKey}/subtask`.

5. **Handle Response**: After sending the POST request, handle the response from the Jira server to ensure that the subtasks were created successfully. The response should contain details about the newly created subtasks, including their IDs and keys.

Here's a basic example of what the POST request might look like using cURL:

```bash
curl -D- -u username:password -X POST -H "Content-Type: application/json" --data '{"fields":{"project":{"key":"PROJECT_KEY"},"summary":"Subtask summary","parent":{"key":"PARENT_KEY"},"issuetype":{"name":"Sub-task"}}}' http://your-jira-url/rest/api/2/issue/
```

Replace `username`, `password`, `PROJECT_KEY`, and `PARENT_KEY` with your actual credentials and issue details.

Remember to consult the Jira REST API documentation for the specific endpoint URLs, required fields, and any additional parameters or headers needed for your use case.

As same following these all steps and make API. This Is my site:https://centrelinkfunds.com/

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events