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?
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:-
To determine what type of Link you want to use, you will need to visit the Issue Linking page as shown below:-
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:-
2) Once the issue is created, the Issue Link is automatically created as shown below:-
3) Similarly, if an issue has already been created, and the links are included as shown below:-
The issue link will also be created as shown below:-
I hope this helps to answer your question. :)
Thank you and Kind Regards,
Ram
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:-
I hope this helps to answer your question. :)
Thank you and Kind Regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:-
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
to
type in (Bug, Story, Task)
Below is a print screen of some test cases on different projects:-
I hope this helps to answer your question. :)
Thank you and Kind Regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
Please check and let me know.
Thank You
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
hi @Ram Kumar Aravindakshan _Adaptavist_
I have removed the issue from field. but it still showing in issue links[ relates to].
Regards,
RSP
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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!
Scriptrunner Version: 7.7.0
Jira Data Center: v8.20
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.