Forums

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

Automation for JIRA - condition to find tickets from a specific value

Romain VELON
Contributor
August 24, 2020

Hi dear community,

I try to create an Automation (with the plugin Automation for Jira that has been bought by Atlassian) with the parameters below:

- Trigger: Value of Field "Text 1" changed

- Then For all issues where field "External ticket Id" = "Text 1"

- Action "Create a link" between the triggered issue and all issues found by the JQL / search

The trigger and the action are quite clear to design. I have more challenge about the condition. I don't know how to use a JQL seach or even a loop condition. Thank you for your help.

Romain

3 answers

1 accepted

0 votes
Answer accepted
Romain VELON
Contributor
September 3, 2020

Hello all,

After new research and internal discussion we found a way to do it with Automation for JIRA. There is an option to compare a field with a value OR with another field

That was exactly what I was trying to achieve. 

Trigger : when field Text 4 changes on my Task issue (or any other field, but it should be the same type than External ticket id that's why I can't use my Number field) 

Branch : search all Enhancement on my project

Condition : the field External ticket Id in that branch should be equal to Text 4 in the trigger issue

Action : link issues

 

The Automation in global

image.png

 

The detailed condition in the branch

image.png

 

Thank you again @Hana Kučerová for your help, I learned much about groovy thanks to you. 

Romain

1 vote
Hana Kučerová
Community Champion
August 24, 2020

Hi @Romain VELON ,

please look at "Branch rule / related issues" component. 

You should be able to select: Type of related issues: JQL ("External ticket Id" = "Text 1")

Then edit all the related issues and setup link for them.

Romain VELON
Contributor
August 24, 2020

Hello @Hana Kučerová ,

Thank you for your answer but it seems it doesn't work. The JQL is considering "Text 1" as a value in a field not as a field itself. 

screenAutomation.jpg

Hana Kučerová
Community Champion
August 24, 2020

Sorry, I didn't understand your problem correctly, I thought it is a value.

As far as I know it is not possible to compare values of fields like this, maybe there are some applications, but I'm not sure, if they will work with Automation for Jira.

Is there any other way, how to identify the related issues?

Romain VELON
Contributor
August 24, 2020

Unfortunately not @Hana Kučerová 

0 votes
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.
August 25, 2020

Hi @Romain VELON 

I believe that you can use the temp variable action to save one field and compare to another.  Here is the explanation of the variables action:

https://community.atlassian.com/t5/Jira-articles/Automation-for-Jira-Create-variable-New-component/ba-p/1448118

Your rule might be something like:

  • Trigger: field value changed for "Text 1"
  • Action: save the value of "Text 1" as a variable (perhaps tempText1)
  • Condition: If/Else to compare {{tempText1}} to "Ticket ID"
    • Action: what you need to do...

 

Best regards,

Bill

Romain VELON
Contributor
August 25, 2020

Hi @Bill Sheboy ,

Thank you for your answer. I believe it would have worked but "Create var" is not available on Server. Seems to be only a Cloud option for now. 

I'm looking at Scriptrunner action if I can create anyhing there.

Romain

Hana Kučerová
Community Champion
August 26, 2020

Hi @Romain VELON ,

if you have ScriptRunner, you can create custom listener for Issue Updated event. Is it suitable for you to have groovy script in your solution? Are you familiar with groovy or should I try to prepare it for you?

Romain VELON
Contributor
August 26, 2020

 

Hi @Hana Kučerová ,

Yes we have Scriptrunner and I'm struggling right now with the script. 

I try to do it in Automation but a Listener should work as well. In 2 words, the algorithm I try to do:

Any time field "Text 1" is updated for issue I1

     Find all issues where "External Ticket Id" equals the Text 1 value from issue I1

     For each issue found

          Link it to I1

     End For

If you're an expert and can help me on that script, I would really appreciate. 

Romain

Hana Kučerová
Community Champion
August 26, 2020

Please try something like this, maybe you will need to do some changes, but it works for me. Just create custom listener - setup your project and event "Issue Updated".

The provided script checks the changes during the issue update and continues only if "Text 1" field has been changed.

Then then JQL is created based on the new value in "Text 1" field.

Issues, where "External Ticket Id" ~ "Text 1" value, are searched (the logged in user is used for the search operation)

If there are some search results, the link between the original issue and found issues is created.

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.query.Query

final String textCustomFieldName = "Text 1"
final String ticketCustomFieldName = "External Ticket Id"
final Long issueLinkTypeId = 10003

def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == textCustomFieldName}
if (!change) {
return
}

JqlQueryParser jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager()
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

Long issueId = event.issue.getId()
String externalTicketId = change.newstring as String
String query = '"' + ticketCustomFieldName + '" ~ "' + externalTicketId + '"'
Query searchQuery = jqlQueryParser.parseQuery(query)

SearchResults<Issue> results = searchService.search(user, searchQuery, PagerFilter.getUnlimitedFilter())
results.getResults().each { result ->
issueLinkManager.createIssueLink(result.getId(), issueId, issueLinkTypeId, 1, user)
}.
 

 

Romain VELON
Contributor
September 1, 2020

Hi @Hana Kučerová ,

Thank you very much, I am very close to the result. I tried to do it in Automation for JIRA instead of Listener. Because my instance is shared with many projects and I prefer to manage this at the project level and not on the instance. 

Anyway you'll find my code below. I don't know why but it seems the program is not able to get the custom field value "Number 1" with the syntax I'm giving. 

  • Trigger of the automation - Field value changed for Number 1
  • Action - Execute Script Runner code

 When I replace 

def number1value = issue.getCustomFieldValue(number1)

by

def number1value = "123456"

this is working correctly (I force the value instead of trying to get it from code). So something is wrong with getCustomFieldValue but I can't see what. 

Thanks for your help.

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.query.Query
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

final Long issueLinkTypeId = 10003


def customFieldManager = ComponentAccessor.getCustomFieldManager()

def number1 = customFieldManager.getCustomFieldObjectByName("Number 1")
def number1value = issue.getCustomFieldValue(number1)
def number1valueString = number1value.toString()

JqlQueryParser jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager()
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

Long issueId = issue.getId()
String query = 'project = MHISAM AND "External Ticket Id" ~ "' + number1valueString + '"'
Query searchQuery = jqlQueryParser.parseQuery(query)

SearchResults<Issue> results = searchService.search(user, searchQuery, PagerFilter.getUnlimitedFilter())
results.getResults().each { result ->
issueLinkManager.createIssueLink(result.getId(), issueId, issueLinkTypeId, 1, user)
}
Hana Kučerová
Community Champion
September 2, 2020

Hi @Romain VELON ,

this is really strange. I've just tested it using my instance and it works.

A few tips, what you could try:

  • getCustomFieldObjectByName is deprecated, use getCustomFieldObject('customfield_xxxxx') instead
  • check there is only one custom field with name "Number 1" in the system
  • add "Re-fetch issue data" action between the trigger and the action

I will try to find out any other possibilites, where the problem could be.

Suggest an answer

Log in or Sign up to answer