Can we create link with current issue using script runner using listener feature
Asked by Riya Badlani:

I have one field 'A' that contains details of issue type 'B'.

when I create and add the value in Field A as A1 and related IssuetypeID - B1

So I want that when issue field A is updated with A2 details old link (i.e.A1) should be update issue and change the issue link as value in custom field A with specific link type like 'ABC'

I have written following script but it is giving error

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.link.IssueLink

import com.atlassian.jira.issue.link.IssueLinkManager

import com.atlassian.jira.user.ApplicationUser

final Issue currentIssue = getIssue(); ApplicationUser currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser() def customFieldManager = ComponentAccessor.getCustomFieldManager() def customField = customFieldManager.getCustomFieldObjectByName("Cost Details") def issueKey = issue.getCustomFieldValue(customField) as String def issueLinkManager = ComponentAccessor.getIssueLinkManager() def issueManager = ComponentAccessor.getIssueManager() def issueToLink = issueManager.getIssueObject(issueKey) /\*for(final IssueLink link: ComponentAccessor.getIssueLinkManager().getIssueLinks(issue.getId())) { if(link.getIssueLinkType().getName().equals("Cost Issue Out")) ComponentAccessor.issueLinkManager.removeIssueLinks(issue, currentUser) } \*/ Collection\<IssueLink\> inwardLinks = issueLinkManager.getInwardLinks(currentIssue.getId()); for(final IssueLink issueLink : inwardLinks) { if(issuelink.getIssueLinkType().getName().equals("Cost Issue Out")) { issueLinkManager.removeIssueLink(issueLink, context.getLoggedInUser()); } } issueLinkManager.createIssueLink([issue.id](http://issue.id/), [issueToLink.id](http://issuetolink.id/), 10840, 1, currentUser)


Answers:
author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Riya Badlani](/t5/user/viewprofilepage/user-id/5191924)

For your requirement, you could try something like this:-

```
import com.adaptavist.hapi.jira.issues.Issues

def issue = event.issue
def currentIssue = Issues.getByKey(issue.key)
def sampleIssuePicker = currentIssue.getCustomFieldValue('Sample Issue Picker')
def inwardLinks = currentIssue.inwardLinks
def linkedIssues = inwardLinks.sourceObject

if (linkedIssues.size() > 0) {
 inwardLinks.each {issueLink ->
 linkedIssues.each { linkedIssue ->
   issue.unlink(issueLink.issueLinkType.inward.toLowerCase(), linkedIssue)
  }
 }
}

if (sampleIssuePicker) {
 currentIssue.update {
 def links = sampleIssuePicker.collect { Issues.getByKey(it as String) }
 setLinks('is blocked by', *links)
 }
}
```

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

Below is a screenshot of the Listener configuration:-

![listener_config.png](https://community.atlassian.com/t5/image/serverpage/image-id/253042iAF5C42519D328310/image-size/large?v=v2&px=999 "listener_config.png")

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

comments: author: Riya Badlani
body: Hi Ram,

Thanks. I will check and let you know if further help is needed.

Best Regards,

Riya Badlani


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Riya Badlani](/t5/user/viewprofilepage/user-id/5191924)

Has your question been answered?

If yes, please accept the solution.

Thank you and Kind regards,

Ram


author: Riya Badlani
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015),

Thanks for asking.

No it is not working for me.

But Resolved it by JWME, Event based action functionality by using different code for create link and execute unlink Issue post function which condition execution.


Tags: scriptrunner