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
Accepted answer

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 Sign up to answer
Community showcase
Posted Oct 16, 2018 in Jira

Looking for anyone who made the switch to Data Center

The Jira Marketing team is putting together an ebook on migrating to Data Center. We're looking for pro tips on how you staffed your project team and organized your Proof of Concept. Share yo...

1,370 views 17 10
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