Forums

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

How to link more than two issues via script based on custom field value?

Edite Araja August 1, 2023

Dear community! I`m new here so hope for your help:)

We have a Scriptrunner Listener for linking Jira issues based on custom field value. This is made for our Agents so that if one customer makes new ticket and in specific (custom) field enters a unique "code", it is as trigger so that previously made tickets and this new one links together - Agents wants' to see related issues that this one customer has made. So if customer will make 3rd ticket, Agent needs to see in all 3 issues in section "Linked issues" all other 2 issues. (For example, we have JIR-1, JIR-2, JIR-3 - in JIR-1 issue I can see as Linked issues JIR-2 and JIR-3, as well as in JIR-2 I can see in Linked issues JIR-1 and JIR-3, and in JIR-3 issue section Linked issues I can see JIR-1 and JIR-2 tickets)

At this moment I have a problem - issues are linking, but I can see all linked issues only in the first made issue - in all issues that are made after the first, in Linked issues section are only one - first one. (For example in JIR-1 issue I can see as Linked issues JIR-2 and JIR-3, but in JIR-2 and JIR-3 tickets in sections Linked Issues I can see only JIR-1 issue)

Could someone help to understand which of the parts in script are incorrect?

import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.search.SearchQuery
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.link.IssueLinkManager

IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager()
IssueManager cm = ComponentAccessor.getIssueManager()
MutableIssue issue = event.issue as MutableIssue;

def linkaId = 10003 //Link type ID
def customerCustomFieldId = 'customfield_10300'; //custom field where customer enters specific and unique code
def customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField customerCustomField = ComponentAccessor.customFieldManager.getCustomFieldObject(customerCustomFieldId);
def customerCustomFieldValue = issue.getCustomFieldValue(customerCustomField) as String;
def projectName = issue.projectObject.name;
def projectId = issue.projectObject.id;

if(customerCustomFieldValue == null){
log.error("There is not filled customer number field");
return;
}

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def jqlSearch = "project = " + """"${projectId}" """ + "AND " + "cf[10300] ~ " + """"${customerCustomFieldValue}" """;
def query = jqlQueryParser.parseQuery(jqlSearch)
def searchQuery = SearchQuery.create(query, user)
def results = searchProvider.search(searchQuery, PagerFilter.getUnlimitedFilter())
def dates = [];
def latestDate;
def latestIssue;
def latestIssueId;

def issueResult = results.getResults()*.document.collect{ComponentAccessor.issueManager.getIssueObject(it.getField('issue_id').stringValue().toLong())};
for (key in issueResult){
dates.add(key.getCreated().toString())
dates.sort()
latestDate = dates.get(0)
if(latestDate == key.getCreated().toString()){
latestIssue = key;
latestIssueId = key.getId()
}
}
issueResult.remove(latestIssue)
if(issueResult != null){
issueResult?.each { it ->
issueLinkManager.createIssueLink(latestIssueId, new Long(cm.getIssueObject(it.toString()).getId()), new Long(linkaId), new Long(0), ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser())
}
}

Maybe it is "latestIssue"? Thanks in advance;)

1 answer

0 votes
Joseph Chung Yin
Community Champion
August 1, 2023

@Edite Araja -

Based on your description "JIR-1 issue I can see as Linked issues JIR-2 and JIR-3, but in JIR-2 and JIR-3 tickets in sections Linked Issues I can see only JIR-1 issue", you need to update your script to ensure links are established between JIR-2 and JIR-3.  Looking at your script, it seems you only established links between JIR1 to JIR2 and 3.

Once the links are established, you should see -

JIR1 is linked to JIR2 and 3

JIR2 is linked to JIR1 and 3

JIR3 is linked to JIR1 and 2

Hope this helps.

Best, Joseph Chung Yin

Jira/JSM Functional Lead, Global Technology Applications Team

Viasat Inc.

Edite Araja August 2, 2023

Dear Joseph,

Thanks for such a quick answer. And Your advice is understandable and logical. But I can't find examples, what exactly I need to change in the script:) Is it just this part? 

{ it ->
issueLinkManager.createIssueLink(latestIssueId, new Long(cm.getIssueObject(it.toString()).getId()), new Long(linkaId), new Long(0), ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser())
}

Could someone point me to the correct direction?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events