Forums

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

how selected issue from issue picker field should get attached in Issue Links of parent issue

RSP
Contributor
September 14, 2021

Hi,

There is multiple issue picker field on screen where I select multiple issues. I need those selected issue to be available in Issue Links.

How can I write listener for this on issue creation and issue update event?

2 answers

1 accepted

1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 15, 2021

Hi @RSP

For your requirement, since you are using the Listeners, you can do something like this:-

import com.atlassian.jira.component.ComponentAccessor

def issue = event.issue

def customFieldManager = ComponentAccessor.customFieldManager
def issueLinkManager = ComponentAccessor.issueLinkManager
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def sampleIssuePicker = customFieldManager.getCustomFieldObjectsByName('Sample Issue Picker').first()
def sampleIssuePickerValue = issue.getCustomFieldValue(sampleIssuePicker).toString()

def issueIds = issueManager.getIssueIdsForProject(10100)
def issues = issueManager.getIssueObjects(issueIds)

issues.each { sourceIssue ->
if(sampleIssuePickerValue.contains(sourceIssue.key)) {
issueLinkManager.createIssueLink(issue.id, sourceIssue.id, 10003, 1, loggedInUser)
}
}

Please note, the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a print screen of the Listener configuration:-

listener_config.png

To determine what type of Link you want to use, you will need to visit the Issue Linking page as shown below:-

Issue_Linking_Config.png

Once you have decided which link type you wish to use, you will need to move your mouse over the Edit link.

You will then see the Issue Link's ID. this is the ID you will need to use in this line of code:-

issueLinkManager.createIssueLink(issue.id, sourceIssue.id, 10003, 1, loggedInUser)

Below are few test print screens:-

1) When the issue is being created, two linked issues are added as shown below:-

test1.png

2) Once the issue is created, the Issue Link is automatically created as shown below:-

test2.png

3) Similarly, if an issue has already been created, and the links are included as shown below:-

test3.png The issue link will also be created as shown below:-

test4.png

 

I hope this helps to answer your question. :)

 

Thank you and Kind Regards,

Ram

RSP
Contributor
September 15, 2021

Hi Ram,

Thank you so much for your response.

But the thing is I want linking should be done from both the sides like as MOCK-6 is showing PCU-8 and PCU-3 in Issue Links same should be done from PSU-6 side need to show MOCK-6 in Issue Links

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 15, 2021

Hi @RSP ,

When a link is created from one issue to another, the issues involved in the link will automatically have the link.

When the link was created in MOCK-6 to PCU-3 and PCU-8, the link was created in MOCK-6 as well as in PCU-3 and PCU-8.

There is no need to do an additional linking config.

Below are the print screens for your reference:-

 

mock_6.png

PCU-3.png

PCU-8.png

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

RSP
Contributor
September 16, 2021

Hi Ram,

I really appreciate your response. Thank you so much.

Still I have one query what if I want to apply this for all projects(Global).

 

Thanks and Regards,

RSP

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 17, 2021

Hi @RSP

To answer your question, if you put it for all projects, i.e. (Global), it will work the same.

This will make sense if you want to use the Issue Picker for all your projects. 

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

RSP
Contributor
September 17, 2021

Hello Ram,

In above listener code you have mentioned project id of one project. So it only work for single project... 

How can I get 'issueIds' of all project. 

def issueIds = issueManager.getIssueIdsForProject(10100)

Thanks,

RSP

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 17, 2021

Hi @RSP,

If you want to do it for all the projects, you need to do several things.

First, change the JQL Query in the Issue Picker to something that is not Project-specific  (i.e. if you want it to contain issues from all projects) for example:-

type in (Story,Bug,Task)

also, as shown in the print screen below:-

issue_picker_config.png

Next, you need to update the Listener code to this:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def issue = event.issue

def customFieldManager = ComponentAccessor.customFieldManager
def issueLinkManager = ComponentAccessor.issueLinkManager
def issueManager = ComponentAccessor.issueManager
def projectManager = ComponentAccessor.projectManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def sampleIssuePicker = customFieldManager.getCustomFieldObjectsByName('Sample Issue Picker').first()
def sampleIssuePickerValue = issue.getCustomFieldValue(sampleIssuePicker) as ArrayList<Issue>

def projects = projectManager.projects
def issueList = [] as ArrayList<Issue>

projects.each {
def issueIds = issueManager.getIssueIdsForProject(it.id)
def issues = issueManager.getIssueObjects(issueIds)
issues.findAll {
issueList.add(it)
}
}

/**
* Include this portion of code if you want to update the
* Linked List when any issue(s) from the issue
* picker is removed
*/
issueList.each {
issueLinkManager.removeIssueLinks(it,loggedInUser)
}

if(sampleIssuePickerValue) {
sampleIssuePickerValue.each {
issueLinkManager.createIssueLink(issue.id, it.id, 10003, 1, loggedInUser)
}
}

I hope this helps to answer your question. :) 

Thank you and Kind Regards,

Ram

RSP
Contributor
September 20, 2021

Hello Ram,

Thank you for your quick response.

Before for specific project it was working.

But  For global project and all issue type it was not working for me. I have changed listener and field code still it was not working.

Regards,

RSP

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 20, 2021

Hi @RSP,

I don't seem to be encountering this problem in my environment.

By setting the Listener to Global (All Projects), it can work on all my projects.

As mentioned in my previous comment, did you ensure that you have updated the Issue Picker's JQL Query and remove the option of which project you want it to work on, and instead specify what issue types you want it to work on, i.e. for example from:-

project = MOCK 

issue_picker_config_old.png

to

type in (Bug, Story, Task) 

issue_picker_config.png

 

Below is a print screen of some test cases on different projects:-

image1.png

 

image2.png

 

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

RSP
Contributor
September 21, 2021

Hi Ram,

I have added the same listener code just changed the field name as "Issue Links".

Still issue not get linked. Before it was working for single project.

Please find the attachments.

 

issue view.png

 

Error.PNG

listener view.pngListener code.png

 

Please check and let me know.

Thank You

RSP
Contributor
September 21, 2021

Hi Ram,

I really appreciate the way you respond with all the details. Thank You so much.

Actually I'm completely new to scripting part. Apologies for the stupid questions.

Thanks and Regards,

RSP 

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 22, 2021

Hi @RSP,

If this is the case, you can remove this portion of the code:-

issueList.each {
issueLinkManager.removeIssueLinks(it,loggedInUser)
}

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

RSP
Contributor
September 23, 2021

Hi Ram,

Script is working completely fine now. Thanks a lot.

But as you have mention in comments if we want to remove the issue what code should I add over there.

Thanks in Advance!

Regards,

RSP

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 23, 2021

Hi @RSP,

I've tested it in my environment, and for it to work fully,  you will need to play around and include some if/else conditions before that function is triggered.

If not, if you just add it as is, whenever an issue is loaded, it will remove the previous links.

Thank you and Kind Regards,

Ram

RSP
Contributor
September 23, 2021

hi @Ram Kumar Aravindakshan _Adaptavist_ 

 

I have removed the issue from field. but it still showing in issue links[ relates to].

test.PNG

Regards,

RSP

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 23, 2021

Hi @RSP

As I mentioned in my previous comment, the code i.e.

issueList.each {
issueLinkManager.removeIssueLinks(it,loggedInUser)
}

is to remove the Linked Issues if the Issue Picker is updated.

However, it doesn't work exactly as it is expected. 

If you test it with the code, it will first display the link when the Issue Picker is selected. However, if you try to reload the page, the Linked Issues will also be removed.

This is why I suggested removing that code.

When unselecting the issues from the Issue Picker,  if you want the Linked Issues also to be removed,  you will need to play around with the code above and include some if / else conditions.

Thank you and Kind Regards,

Ram

Aaron Coonce January 4, 2023

@Ram Kumar Aravindakshan _Adaptavist_ 


I am doing these steps for a single project. It is working great! However, when I select my sample picker value of Media-80 it links both 80 and 8! Any ideas?

import com.atlassian.jira.component.ComponentAccessor

def issue = event.issue

def customFieldManager = ComponentAccessor.customFieldManager
def issueLinkManager = ComponentAccessor.issueLinkManager
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def VersionReplacment = customFieldManager.getCustomFieldObjectsByName('Version Replacement').first()
def VersionReplacementPickerValue = issue.getCustomFieldValue(VersionReplacment).toString()

def issueIds = issueManager.getIssueIdsForProject(10001)
def issues = issueManager.getIssueObjects(issueIds)

issues.each { sourceIssue ->
    if(VersionReplacementPickerValue.contains(sourceIssue.key)) {
        issueLinkManager.createIssueLink(issue.id, sourceIssue.id, 10300, 1, loggedInUser)
    }
}




Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 8, 2023

Hi @Aaron Coonce

Could you please share a screenshot of the issue link?

Also, could you please confirm the version of ScriptRunner and Jira that you are testing with? I am asking this because, in your comment, you mentioned:-

However, when I select my sample picker value of Media-80 it links both 80 and 8! Any ideas?

What you have mentioned seems to be an old bug which was fixed and released. Hence, I need to confirm if you are using an earlier version which doesn't contain the fix.

I am looking forward to your feedback.

Thank you and Kind regards,
Ram

Aaron Coonce January 12, 2023

@Ram Kumar Aravindakshan _Adaptavist_ 

Ram,

I appreciate your assistance I actually got it to work! I took your formula and adjusted it a little! Thanks for solid explanations!

 

Community.PNG

Scriptrunner Version: 7.7.0
Jira Data Center: v8.20

1 vote
Helmy Ibrahim _Adaptavist_
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 11, 2023

To anyone looking at this post in 2023, we recently released HAPI early this year, and the script now can be as simple as below:

def source = event.issue

def destination = source.getCustomFieldValue('Issue Picker Field')

if (destination) {
source.link('blocks', destination)
}

Cheers,
Helmy

Suggest an answer

Log in or Sign up to answer