How to copy multi-value field in using Advance Options in Automation Rule?

Arun Makhija April 9, 2018

In an automation rule, created a sub-task and copying a multi-value field like labels/components/fix version etc. from Parent to the child sub-task

I tried both below to set labels from the Parent issue,

"labels": "{{issue.labels}}"

"labels": [ "{{#issue.labels}} {{.}}{{/}}"]

but if the parent has multiple values in the Labels field, it errors out

Error creating issue - The label 'Label1, Label2, Label3' contains spaces which is invalid. (labels)

Doing this graphically works but trying to achieve it via Advance options using JSON

How do you copy a multi-value field using Advance Options in Automation Rule?

any help is appreciated

~Arun

2 answers

5 votes
Erik Buchholz
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.
July 16, 2019

The problem is, that you need to use the following syntax for arrays.

{
"fields": {
"labels": ["label_1", "label_2"]
}
}

You can achieve this syntax easy with the following smartvalues {{labels.asJsonStringArray}}

So one solution is the following code.

{
"fields": {
"labels": {{labels.asJsonStringArray}}
}
}

If you want to do it with the list syntax you can use the following code.

{
"fields": {
"labels": [{{#labels}}"{{.}}"{{^last}},{{/}}{{/}}]
}
}

With this code you enclose every label with ' " ' and seperate them with ' , '

More information about the asJsonStringArray and similar options you can find at

https://docs.automationforjira.com/issues/edit-additional-fields.html#using-smart-values

there is also a reference for other fields like custom fields.

About the list options the reference is at

https://docs.automationforjira.com/reference/lists.html#list

PS: Thanks to Victor Seger from Automation for Jira for giving me the right input on my problem with a custom field.

Daniel Oriold July 26, 2021

Really helpful, thanks. But how do I add these Labels to existing labels? This currently removes existing labels

Like Erik Buchholz likes this
Erik Buchholz
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.
July 27, 2021

Hi @Daniel Oriold ,

that's right. "fields" is an alias for update > set, so you overwrite existing values. That's no problem if you create a new issue in the automation but may be, if you want to update an existing one.

If you want to add some labels, you need to use the update syntax like

{
"update": {
"labels": [{"add": "label_1"}, {"add": "label_2"}]
}
}

This syntax is a bit more complicated than the "fields" syntax, what makes us need to change the other code examples, too. I thought we could use the asJsonObjectArray("add") function to get it the simple way, but that doesn't give us anything.

So we need to use list syntax:

{
"update": {
"labels": [{{#labels}}{"add": "{{.}}"}{{^last}},{{/}}{{/}}]
}
}

Please note that this gives an error if the issue has no labels. To cover this case you could add an {"add":""} at the beginning and move the comma.

{
"update": {
"labels": [{"add":""}{{#labels}},{"add": "{{.}}"}{{/}}]
}
}

Regards

PS: If you like my answer, I'd appreciate if you upvote it. Thanks!

Like # people like this
Simone Kaiser July 28, 2021

Thank you very much! This was a great help :)

Andreas Lärkfeldt February 9, 2023

Awesome - such a small task however annoying! This solved it :D TY!

Like Erik Buchholz likes this
Kalyan Sattaluri
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.
December 5, 2023

Thanks @Erik Buchholz  This was great.

0 votes
Daniel Oriold July 27, 2021

Hey Erik, I am actually using a custom field call 'Segment' which is a Select List (multiple choices). I want the automation to copy the values in the Segment and add them as individual Labels to the Labels field. When I do this:

{
"update": {
"labels": [{"add":""}{{#segment}},{"add": "{{.}}"}{{/}}]
}
}

Then I get the following Error

('Field 'labels' cannot appear in both 'fields' and 'update')

Erik Buchholz
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.
July 27, 2021

Hi @Daniel Oriold ,

the error says that you still have labels in the fields part of the JSON. Is that just the old code or do you try to do something different there?

Regards

Like Daniel Oriold likes this
Daniel Oriold July 28, 2021

Super weird. Now all of a sudden it is working. You have no idea how helpful this is to so many of our users. Thanks so much. I wish I could buy you a beer...

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events