How to get current issue when I use IssueLinkCreatedEvent event

Paweł Jakimiak
Contributor
August 30, 2018

Hi,

I created a script that sets specified value in Y custom field when I set specified value in X field.
X field is "Epic Link" (type: Epic Link Relationship) and Y it "Organizational Unit" field (type: Select List (single choice).
When I set constant issue key, script works correctly, but when I place script in Script Listener and set Events to "IssueLinkCreatedEvent" my script don't see a current issue.
In console I get information about null object.

How I can get current editing issue in this case?

Actually I try get current issue by code:
Issue issue = issue.

I tried also this method:
Issue issue = (Issue)jiraHelper.getContextParams().get("issue");

but that did not help.

 

3 answers

1 accepted

4 votes
Answer accepted
Mark Markov
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.
September 4, 2018

Hello @Paweł Jakimiak

In listeners you can get issue from event, like this

Issue issue = event.issue
Paweł Jakimiak
Contributor
September 4, 2018

Unfortunately, I receive this error:

2018-09-04 13:50:57,414 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2018-09-04 13:50:57,430 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.link.IssueLinkCreatedEvent, file: <inline script>
groovy.lang.MissingPropertyException: No such property: issue for class: com.atlassian.jira.event.issue.link.IssueLinkCreatedEvent
 at Script316.run(Script316.groovy:9)
Mark Markov
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.
September 4, 2018

Oh, this is IssueLink event.

Is contains IssueLink object, which have methods to retrieve source and destination issues

Issue destinationIssue = event.getIssueLink().getDestinationObject()
Issue sourceIssue = event.getIssueLink().getSourceObject()
Like Darshan.VPrasad likes this
Paweł Jakimiak
Contributor
September 4, 2018

Thank you very much for your help. It's works.

Aditya Wijanarko March 5, 2019
Issue destinationIssue = event.getIssueLink().getDestinationObject()
Issue sourceIssue = event.getIssueLink().getSourceObject()

I use this code, but it doesn't work for me. 

Untitled.png

Can anybody help?

Eric Lakey April 11, 2019

I'm getting this error too.  Has anyone solved this?

Damian Wodziński July 8, 2019

@Eric Lakey @Aditya Wijanarko 

I managed to resolve it:

Issue issue = null; //Indicates a new Issue Object, without this event. doesn't seems to work

if(event.getClass().toString() == "class com.atlassian.jira.event.issue.link.IssueLinkCreatedEvent"){ // Need some way to check if link is created or deleted, i saw something like this: (event instanceOf issueLinkCreatedEvent) but this didn't work for me
def event = event as IssueLinkCreatedEvent
issue = event.getIssueLink().getSourceObject()
} else if(event.getClass().toString() == "class com.atlassian.jira.event.issue.link.IssueLinkDeletedEvent"){
def event = event as IssueLinkDeletedEvent
issue = event.getIssueLink().getSourceObject()
} else issue = issue; //In my code, I am using also "Issue updated" event, if you want only check if Link is created or deleted, just erase this
Like Asaf.Porat likes this
Daniel
Contributor
August 26, 2019

Hi @Damian Wodziński 

Did that code work for you? Your comments are saying something differently and I get a:

unable to resolve class IssueLinkCreatedEvent

Can you also post your import section?

Best
Daniel

Daniel
Contributor
September 4, 2019

Now I know. What is often missing for beginners - which class to import. In this case do

import com.atlassian.jira.event.issue.link.IssueLinkCreatedEvent
import com.atlassian.jira.event.issue.link.IssueLinkDeletedEvent
Rolf Lader
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 14, 2022

@DanielI get the following error although I have inserted the two import statements.

2022-03-14 12:50:21,858+0100 http-nio-8080-exec-6 DEBUG rlader 770x1674x1 2ytux2 172.24.15.3,172.18.7.1 /secure/DeleteLink.jspa [jira.transport.system] A link event was found
2022-03-14 12:50:21,887+0100 http-nio-8080-exec-6 ERROR rlader 770x1674x1 2ytux2 172.24.15.3,172.18.7.1 /secure/DeleteLink.jspa [c.o.scriptrunner.runner.AbstractScriptListener] *************************************************************************************
2022-03-14 12:50:21,887+0100 http-nio-8080-exec-6 ERROR rlader 770x1674x1 2ytux2 172.24.15.3,172.18.7.1 /secure/DeleteLink.jspa [c.o.scriptrunner.runner.AbstractScriptListener] Script function failed on event: com.atlassian.jira.event.issue.link.IssueLinkDeletedEvent, file: null
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'com.atlassian.jira.event.issue.link.IssueLinkDeletedEvent@5e41046a' with class 'com.atlassian.jira.event.issue.link.IssueLinkDeletedEvent' to class 'com.atlassian.jira.event.issue.link.IssueLinkCreatedEvent'
at TransportSystemListener.run(TransportSystemListener.groovy:53)

 Do you have any idea what is causing this?

Like Aditya Wijanarko likes this
Rolf Lader
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 14, 2022

@Aditya Wijanarko I have solved the problem by paying exact attention to whether I am processing an 'IssueLinkCreatedEvent' or an 'IssueLinkDeletedEvent' event. Otherwise I'm geting the above error.

1 vote
Thierry Dalon
Contributor
October 6, 2022

The accepted answer does not answer the question.

Issue destinationIssue = event.getIssueLink().getDestinationObject()
Issue sourceIssue = event.getIssueLink().getSourceObject()

I couldn't find a way to identify which of the edited current issue is the source or the destination of the IssueLink.

 

The Event object does not provide the information from which issue it was triggered.

Rolf Lader
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 7, 2022

Hi @Thierry Dalon

From my understanding it is irrelevant from which issue the modification was done. Can you explain the reason why it is for you relevant from which issue the link modification was done?

Like # people like this
Thierry Dalon
Contributor
October 7, 2022

Rethinking about it yes you are right this is irrelevant: the link can be created from both sides.

See also related post here: https://community.atlassian.com/t5/Jira-questions/IssueLinkCreatedEvent-doesn-t-have-either-parent-issue-or-issue/qaq-p/1274166#M552297

Like # people like this
0 votes
reza azad January 7, 2019

hi i have this problem to, i want to delete issue link when user want to create link to another project,and user only can link issue to current project that issue is in,i try this code 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.event.issue.EventUtils

//import com.atlassian.jira.event.issue.link.IssueLinkCreatedEvent
Issue issuedes = event.issue;
Issue issuesou;

ProjectManager projectMgr = ComponentAccessor.getProjectManager();
//Issue destinationIssue = IssueLink.getDestinationObject()
//Issue sourceIssue = IssueLink.getSourceObject()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
Issue currentIssue = issuesou
//def currentProject = currentIssue.projectObject.name
def linkedIssue = issuedes
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

issueLinkManager.getOutwardLinks(currentIssue.id).each { issueLink ->

linkedIssue = issueLink.getDestinationObject()

if( linkedIssue.getProjectObject().getKey() != currentIssue.getProjectObject().getKey() )
{

issueLinkManager.removeIssueLink(issueLink, user)
}

}

 

and i have this error 

 

2019-01-08 11:21:03,878 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2019-01-08 11:21:03,878 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
java.lang.NullPointerException: Cannot get property 'id' on null object
 at Script3086.run(Script3086.groovy:23)

i think i can not refer to sourceIssue and  event.issue; code is for destinationIssue,

please help me thank

Damian Wodziński July 8, 2019
Issue issuesou; 

This indicated null object

Then:

Issue currentIssue = issuesou

 

So when you try to do this:

issueLinkManager.getOutwardLinks(currentIssue.id).each

It causes the problem, it cannot do anything on null object.

Suggest an answer

Log in or Sign up to answer