Hi everybody!
I am trying to create a table using JIRA Automation in the comments to an issue. Basically when an issue is created it should add to the comments a table with related issue using a JIRA query (Lookup Issues).
When I pass this as a comment:
The host "{{issue.customfield_xxxxx}}" was seen in these tickets in the past 30 days: | Key | Field_1 | Field_2 | Field_3 | Field_4 | {{#lookupIssues}} | {{key}} | {{fields.customfield_xxxx}} | {{fields.customfield_xxxx}} | {{fields.customfield_xxxxx}} | {{fields.customfield_xxx}} | {{/lookupIssues}}
It tries to display all issues (using the default limit of up to 100) and the maximum characters limit is exceeded, so the comment can't be added.
How can I limit the table only to the first 10 issues?
Hi @Mark Taranenko -- Welcome to the Atlassian Community!
JQL is not an SQL, and thus it does not have features to return a specific number of issues. There may be marketplace addons to JQL which provide this capability, although I have not used any of them.
For the scenario you describe, I know three ways to get a specific number of results (e.g., 10 issues) from the Lookup Issues action results. I have listed them in level of complexity / effort.
The results of the Lookup Issues action are a list, and so the get() function may return a specific item. To return the first 10, use a series of get() calls, where the first issue's index is 0 and the tenth is 9:
{{#lookupIssues.get(0)}}
| {{key}} | {{customfield1}} | {{customfield2}} |
{{/}}
{{#lookupIssues.get(1)}}
| {{key}} | {{customfield1}} | {{customfield2}} |
{{/}}
...
{{#lookupIssues.get(9)}}
| {{key}} | {{customfield1}} | {{customfield2}} |
{{/}}
To prevent errors when there are fewer than 10 issues found, conditional logic may be used to test for the presence of each item. And, remember to add an ORDER BY clause to your JQL to get consistent results.
Automation rules use the REST API to perform many of their actions, including Lookup Issues. Rather than using that action, a rule could call the REST API endpoint for an issue search with JQL and pass the maxResults parameter. That could be set to 10 to return exactly what is needed. Calling the endpoint is done using the Send Web Request action. Here is some information if you want to try this technique:
how-to article for calling a REST API from a rule: https://community.atlassian.com/t5/Jira-articles/Automation-for-Jira-Send-web-request-using-Jira-REST-API/ba-p/1443828
the REST API endpoint for issue search with JQL: https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-search/#api-rest-api-3-search-jql-get
A more complicated method is to expand the Lookup Issues results into a created variable, including the {{index}} as the list is iterated. Then use a dynamic regular expression to search the list with the match() function and return the final list.
To learn more about this method, please study this article: https://community.atlassian.com/t5/Automation-articles/Automation-concepts-Dynamic-searches-within-a-list/ba-p/2834235
Kind regards,
Bill
Thank you, Bill!
The first variant was exactly what I needed!
Good to now about other solutions as well
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Instead of limiting the results to an arbitrary value - 10 - consider refining your Lookup Issues action to search for a tighter query? For example, issues created within the last 3 days. (Yes, that’s arbitrary too 😂 )
Can you share how your Lookup Issues action is configured?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It has a JQL that searches for tickets with the same custom_field value in a project for the last 30 days. I can't change it to 3 days, cause the whole purpose of this automation is to retrieve related tickets from the past month
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Got it!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.