Forums

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

How can I print each item in a list using smart values

Johan Markström
Contributor
January 9, 2023

Fellow Atlassians!

 

I'm creating an automation rule that triggers a web hook with a POST request to the Confluence REST API to create a new Confluence page with certain data from an issue. 

 

Everything is working fine (at last!) but as a next step I wanted to add the components of the issue as labels to the Confluence page. Using 

"metadata": {
"labels": [

{
"name": "{{issue.components.name}}"
}
]
}

 I'm succesfully adding the component as a label to the Confluence page for issues only associated with one component, but I'm getting an error when the issue has multiple components. 

{"statusCode":500,"message":"java.lang.IllegalArgumentException: Invalid label 'automations & workflows, boards & filters, permissions'"}

From the error I'm getting, I'm guessing there's a max characters limit or something for labels in Confluence, but it could also be a formatting error due to {{issue.components.name}} now returning a list, but I have no clue and would appreciate elaborations and duscussions on the matter! 

 

 Anyways, in this support documentation page, under "Using smart values with sections and lists" there's a brief mention of 

{{#issue.comments}}Author: {{author.displayName}}{{/}}

 returning the comment authors of a given issue. I've tried this syntax for components, but it doesn't seem to work. I don't understand what the {{#}} {{/}} functionality does, so if someone would like to elaborate on this as well, it would be very appreciated. Could this achieve what I want it to?

 

Sorry for a lengthy question, I wanted to provide some context to possibly receive input on other solutions and ideas.

 

Thanks! 

2 answers

1 accepted

2 votes
Answer accepted
Bill Sheboy
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.
January 9, 2023

Hi @Johan Markström 

At the bottom of each of the automation documentation pages, it often lists longer examples, like this one for lists: https://support.atlassian.com/cloud-automation/docs/jira-smart-values-lists/#Combined-function-examples

The {{#somelist}} starts an iterator to walk a list and the {{/}} closes the iterator.  These can be nested and filtered as needed.

And this is a good example for the JSON to add multiple entries, like for component with a list: https://support.atlassian.com/cloud-automation/docs/advanced-field-editing-using-json/#Components

Putting those ideas together, you can can add the multiple labels per component like this:

"metadata": {
"labels": [
{{#issue.components}}
{
"name": "{{name}}"
},
{{/}}
]
}

This is also possible with the JSON functions, if you want to try that: https://support.atlassian.com/cloud-automation/docs/jira-smart-values-json-functions/

Kind regards,
Bill

Johan Markström
Contributor
January 17, 2023

I got it to work now! There didn't seem to be any syntactical errors in my Json, but I didn't use the smart values correctly. The method that worked for me was the following: 

 

"metadata": {
"labels":
{{issue.components.asJsonObjectArray("name")}}
}

 

Removing the brackets [] from "labels" and letting the Json function provide the complete array instead. 

 

In my previous attempt using this method, I incorrectly left the {{#issue.components}} loop around the Json function, resulting in a complete array of all components, for every component in the array.  

 

Thank you for providing the documentation regarding the Json functions for Jira smart values, it saved my day! 

 

Have a good one!

Like Bill Sheboy likes this
Bill Sheboy
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.
January 17, 2023

Well done!

For rules like this, it can help to initially write the automatically generated content to the audit log.  Then you can confirm the results match what you expect before using the webrequest.

Like Johan Markström likes this
0 votes
Johan Markström
Contributor
January 11, 2023

Hi @Bill Sheboy !

 

Thanks for your reply and in depth explanation. I tried your example, and some solutions of my own using the Json functions you provided. This is the full json object I'm trying to post to the Confluence API:

 

{
    "type": "page",

    "title": "{{issue.key}} {{issue.summary}}",

    "ancestors": [

        {
            "id": "SomeID"
        }
    ],

    "space": {

        "key": "SomeKey"

    },

    "body": {

        "storage": {

            "value": "<H3>Description</H3>{{issue.description.htmlEncode}}<h3>Solution</h3>{{issue.Solution.htmlEncode}}",

            "representation": "storage"
        }
    },

    "metadata": {

        "labels": [

            {{#issue.components}}
{

                "name": "{{name}}"

}
            {{/}}
        ]
    }
}

 

This produces the following error: 

"Unexpected character (']' (code 93)): expected a value\n "

 

The following example doesn't produce any errors, but it also doesn't add labels to the document. I'm guessing it's because the json function only produces a nested array. 

 "labels": [
{{#issue.components}}
{{name.asJsonObjectArray("name")}}
{{/}}
]

I tried "unnesting" it by removing the brackets for the labels array, but this produced an error: 

 "labels":
{{#issue.components}}
{{name.asJsonObjectArray("name")}}
{{/}}

Error: Unexpected character ('}' (code 125)): expected a value

 

I've also tried using the asJsonObject() function, but it gives the following error: 

"Unexpected character ('{' (code 123)): was expecting comma to separate Array entries\n"

I can guess why this error is occuring, but I don't know where to insert the comma for it to work.

 

Appreciate your help! 

 

Johan

Bill Sheboy
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.
January 11, 2023

Hi!

First thing, a disclaimer: I have not tried adding a page from a rule before.  That noted...

Next, please try to respond in the same conversation thread.  That will help when others are looking for answers in the future so they can distinguish different solution approaches.  Thanks!

Okay...have you tried hard-coding a label to see if that works with your rule?  I ask because what you show does not seem to match the syntax for label metadata, such as shown here: https://developer.atlassian.com/cloud/confluence/rest/v1/api-group-content/#api-wiki-rest-api-content-post

Johan Markström
Contributor
January 12, 2023

Naturally! I simply selected the wrong field when replying.

 

Yes it does work when I hard code a label like so: 

 "metadata": {
"labels": [
{
"name": "SomeName"
}

 

Could you be more detailed about what might be wrong in my syntax? Are you referring to line 77 in the Confluence Rest API, where it states: 

"metadata": 
{ "labels":
{ "results": [
{
"prefix": "<string>",
"name": "<string>",
"id": "<string>",
"label": "<string>"
}
],
"size": 2154 },
 

 

I've tried putting the name in a results array, but this doesn't print the hard coded label to the page.

 

Thank you!

Bill Sheboy
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.
January 13, 2023

Please try to add multiple labels manually first.  That will confirm you have the correct syntax, and then we can adjust to add the components dynamically.

Again, I have not tried this, and...I was noting the differences in the inadequate documentation page versus what you were trying.  I haven't found a reference yet that shows an example update instead of a template for get-content JSON.

Suggest an answer

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

Atlassian Community Events