How Do I Using Adaptavist ScriptRunner could remove all linked issues and then add one? Edited

Could someone advise please, how could I delete all linked issue when I change a value of custom filed (for ex. SOW) and then add linked issue mentioned in that field? 
I've tried that code, but it doesn't work at all:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript
import groovy.transform.Field

@BaseScript FieldBehaviours fieldBehaviours
MutableIssue currentIssue = issue
@Field
String SOW_CF_ID = 'customfield_21900'
FormField sow = getFieldById(SOW_CF_ID)
ApplicationUser currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

if (sow.value) {
ComponentAccessor.issueLinkManager.removeIssueLinks(issue, currentUser)
}

First of all, because jira doesn't understand what issue it is.

 

 

1 answer

1 accepted

0 votes
Joshua Yamdogo Community Champion Oct 09, 2017

Hello Aleksey,

The problem with your script is that you need to get the underlyingIssue (not just issue) in order to use this in a behaviour. Also, removeIssueLinks() does not accept a MutableIssue as a parameter, so you shouldn't define currentIssue as a MutableIssue.

This is the script that worked for me to remove linked issues:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript
import groovy.transform.Field

@BaseScript FieldBehaviours fieldBehaviours
def currentIssue = underlyingIssue
@Field
String SOW_CF_ID = 'customfield_21900'
FormField sow = getFieldById(SOW_CF_ID)
ApplicationUser currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

if (sow.value) {
ComponentAccessor.issueLinkManager.removeIssueLinks(issue, currentUser)
}

I would also say that I really do not think this should be done in a behaviour. A script listener would be a better fit for this use-case. You could set up a listener that fires on an Issue Updated  event. If someone changes the custom field, the listener will delete all issue links once the change to the custom field is made.

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,754 views 11 18
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot