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

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

189 views 1 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