Forums

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

Creating Subtasks with an existing parent Issue using JIRA REST API

Jayanthi July 14, 2023

I need to create Subtasks with an existing parent Issue using JIRA REST API through Excel VBA. Can anyone share me a sample code in VBA

2 answers

1 accepted

2 votes
Answer accepted
Sayed Bares _ServiceRocket_
Community Champion
July 15, 2023

@Jayanthi are you using Jira Server/DC or Jira Cloud?

For Jira Server you can refer to the following documentation:

For Jira Cloud you can look into:

The documentation already has examples which you can use to develop your own code.

Hope it helps!

0 votes
Evgenii
Community Champion
July 15, 2023

Hi, @Jayanthi 

Not sure about this script, that it will work right as you need (I'm not a fan of VBA), but you can give it a try.

Certainly! Here's a sample VBA code that demonstrates how to create subtasks for an existing parent issue using the Jira REST API in Excel VBA:

```vba
Sub CreateSubtasks()
Dim parentIssueKey As String
parentIssueKey = "PARENT-123" ' Replace with the key of the parent issue

Dim subtaskSummaries As Variant
subtaskSummaries = Array("Subtask 1", "Subtask 2", "Subtask 3") ' Replace with the summaries of your subtasks

Dim subtaskDescriptions As Variant
subtaskDescriptions = Array("Description 1", "Description 2", "Description 3") ' Replace with the descriptions of your subtasks

Dim jiraURL As String
jiraURL = "https://your-jira-instance.atlassian.net/rest/api/2/issue/"

Dim authHeader As String
authHeader = "Authorization: Basic " & Base64Encode("your-jira-email@example.com:your-jira-api-token")

Dim http As Object
Set http = CreateObject("MSXML2.XMLHTTP")

Dim i As Long
For i = LBound(subtaskSummaries) To UBound(subtaskSummaries)
Dim subtaskSummary As String
subtaskSummary = subtaskSummaries(i)

Dim subtaskDescription As String
subtaskDescription = subtaskDescriptions(i)

Dim payload As String
payload = "{""fields"": {""project"": {""key"": ""YOUR-PROJECT-KEY""},""summary"": """ & subtaskSummary & """,""description"": {""type"": ""doc"",""version"": 1,""content"": [{""type"": ""paragraph"",""content"": [{""type"": ""text"",""text"": """ & subtaskDescription & """}]}]},""issuetype"": {""name"": ""Sub-task""},""parent"": {""key"": """ & parentIssueKey & """}}}"

Dim url As String
url = jiraURL

http.Open "POST", url, False
http.setRequestHeader "Content-Type", "application/json"
http.setRequestHeader "Accept", "application/json"
http.setRequestHeader "Authorization", authHeader

http.send payload

If http.Status = 201 Then
MsgBox "Subtask created successfully."
Else
MsgBox "Failed to create subtask. Error: " & http.responseText
End If
Next i

Set http = Nothing
End Sub

Function Base64Encode(ByVal input As String) As String
Dim objXML As Object
Dim objNode As Object
Set objXML = CreateObject("MSXML2.DOMDocument")
Set objNode = objXML.createElement("base64")
objNode.DataType = "bin.base64"
objNode.nodeTypedValue = Stream_StringToBinary(input)
Base64Encode = objNode.Text
Set objNode = Nothing
Set objXML = Nothing
End Function

Function Stream_StringToBinary(ByVal s As String) As Variant
Dim ado As Object
Dim stm As Object
Set ado = CreateObject("ADODB.Stream")
ado.Type = 2 ' adTypeText
ado.Charset = "us-ascii"
ado.Open
ado.WriteText s
ado.Position = 0
ado.Type = 1 ' adTypeBinary
Stream_StringToBinary = ado.Read
ado.Close
Set stm = Nothing
Set ado = Nothing
End Function
```

Make sure to replace the following values in the code:

- `parentIssueKey`: The key of the parent issue in Jira.
- `subtaskSummaries`: An array containing the summaries of the subtasks you want to create.
- `subtaskDescriptions`: An array containing the descriptions of the subtasks you want to create.
- `jiraURL`: The base URL of your Jira instance's REST API.
- `authHeader`: Replace `"your-jira-email@example.com:your-jira-api-token"` with your Jira email and API token.
- `YOUR-PROJECT-KEY`: The key of your Jira project.

Please note that this code uses basic authentication with your Jira email and API token. Make sure to replace it with the appropriate credentials and adjust the Jira URL according to your instance.

Before running the code, ensure that you have enabled the "Microsoft XML, v6.0" reference in your VBA editor by going to "Tools" > "References" and checking the corresponding option.

When you run the `CreateSubtasks` macro, it will create subtasks for the specified parent issue using the provided summaries and descriptions. A message box will display the success or failure of each subtask creation attempt.

Remember to customize the code according to your specific Jira configuration and requirements.
Jayanthi July 15, 2023

Thanks a lot. Will work on this and revert. But this is definitely useful

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, jira service management, jsm webinar, ai in jsm, opsgenie integration, incident management, virtual agent, atlassian intelligence, ai-powered service desk, it operations, atlassian learning, service management webinar, team '25 recap

What’s new in Jira Service Management 🤔

Discover how Atlassian is revolutionizing service management with cutting-edge solutions for AI-powered support, HR Service Management, or DevOps connectivity.

Register here ⬇️
AUG Leaders

Atlassian Community Events