Creating issues in Automation For Jira when you need to set the Project dynamically

C_ Derek Fields
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 Leaders.
September 7, 2023

Recently, I had a requirement to create a series of linked issues in different projects as part of the automation for a client's business process. Being a software developer, it pains me to write the same code over and over, like a list of "Create Issue" and "Link Issue" actions, each identical except for the target project. Not only is this incredibly ugly, but it is hard to maintain when you need to change something, like adding a new field to the new issue or changing the link type.

What I wanted to do was to have a list of Project Keys and loop through them to create the linked issues. There is just one problem - you can't specify a Project Key in the Create Issue action. You have to provide the Project Id, which can be hard to find and makes maintenance even more difficult. 

Just to make things a little more complicated, I wanted to specify a different Summary for each issue. Here is the solution that I came up with. I am sharing it in the hope that this will save you some frustration and encourage you to share some of your more creative solutions.

The first step is to create a variable called "Tasks" from a string of Summary/Project. My string format looked like this: "Summary1:Project1;Summary2:Project2;Summary3:Project3" Note that I separate the Summary from the Project by a colon ":" and the Summary:Project set from each other by a semicolon ";". You can use any separator you want. This was just what I chose.2023-09-07_08-11-42.jpg

 

Step 2 is an Advanced Branch in which I loop over the Summary:Project items by passing the smart value {{Tasks.split(";")}} to a new variable that I called "Task" (singular). Now I have the Summary:Project pair in my new variable.

2023-09-07_08-01-52.jpg

Step 3 is to call a Webhook that returns the Project Id. The URL is <Jira Base>/rest/api/3/project/{{Task.split(':').last}}. Notice the "last" at the end. This splits the Summary:Project into a first and last part. Also, it is essential that you check the "Delay execution..." box so that it waits for the result of the GET call. 

2023-09-07_08-02-47.jpg

Step 4 is to create a variable called ProjectId from the webResponse that comes back from Step 3. ProjectId is created from {{webResponse.body.id}}, which is the Project Id. It is not required to create this variable as you can access the information directly, but I think it makes the routine somewhat more readable.2023-09-07_08-03-01.jpg

Step 5 is to create the new Issue using the {{ProjectId}} key. I set the Summary to {{Task.split(':').first}}. In my case, I also copy a bunch of fields from the Trigger issue.2023-09-07_08-03-25.jpg

Finally, Step 6 is to link the newly created issue to the original issue. This is straightforward. 2023-09-07_08-03-39.jpg

 

 

2 comments

Comment

Log in or Sign up to comment
Judd Garratt - Path of Trees
Atlassian Partner
September 10, 2023

Thanks for sharing this :) I was trying to setup something similar across projects for someone the other week. Your solution is what I needed.

Haddon Fisher
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 Leaders.
October 17, 2023

I haven't tried this in a bit so it's always possible they broked it, but I thought it was possible to create issues in Automation with project being set dynamically; you just need to use the JSON advanced editor to "override" the project set in the picker.

The JSON I have used in the past was:

{
"fields": {
"project":{
"key": "{{project}}"
}
}
}

In this example, I'm actually setting a variable for "project" out of a custom field value. If anyone is curious, each value has the convention "Project Name (Project Key)" and then I use the following regex to suss out the key to use:

{{#issue.Participating Teams}}{{value.replaceall("(.*)(\()(.*)(\))","$3")}}{{/}}
TAGS
AUG Leaders

Atlassian Community Events