Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Set Allowed Link Types script not working after ScriptRunner upgrade

Diana
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.
February 8, 2023

I followed this script exactly: https://library.adaptavist.com/entity/control-link-directions

But ever since I upgraded ScriptRunner to 7.9.0, I get an error under setFieldOptions(allowedLinks)

"Cannot call com.onresolve.jira.groovy.user.FormField#setFieldOptions(java.util.Map <?, java.lang.String>) with arguments [java.util.Map <java.lang.Object, java.lang.Object>]"

I also tried using https://gist.github.com/qoomon/d49560c7ef805ad409f545ed4964d5c8 but there are still all link types that are showing up when Creating or Editing an issue. What do I need to do to update this script to work?

We have a link type name "MVP" and only a few issue types are allowed to use the outward link for that link type.

1 answer

1 accepted

0 votes
Answer accepted
PD Sheehan
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.
February 8, 2023

In a test instance running Scriptunner 7.10, I was able to make the script from the library run as is without any changes.

The error in the editor can be alleviated by explicitly casting the allowedLinks in the last line:

linkTypesField.setFieldOptions(allowedLinks as Map<String,String>)

But that was not a breaking error. Just a warning. The code will run fine even if you don't change it.

Make sure the allowedOutwardTypesNames and allowedInwardTypesNames match your inward and outward descriptions from your issue links.

Diana
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.
February 9, 2023

Hi @PD Sheehan

Thanks for the quick response!

that last line edit does help remove the error, but the script still doesn't work. I have a link name "MVP" with outward direction "is MVP for" and inward direction "has MVP". We have all our issue types are allowed to use all links, except only 1 issue type is allowed to use "has MVP" and none of the other issue types can use it.

I checked the log.debug and it does show "Allowed Links = [blocks:block, clones:clones,...is MVP for:is MVP for] and the list excludes the "has MVP", so i assume that means it works. However, I can still select "has MVP" and update and issue using that link type name.

But just in case, I have my script as:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.onresolve.jira.groovy.user.FieldBehaviours
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours
def log = Logger.getLogger(getClass())
log.setLevel(Level.DEBUG)

def linkTypesField = getFieldById("issuelinks-linktype")

def allowedOutwardTypesNames = ["blocks", "relates to", "causes","is MVP for"]
def allowedInwardTypesNames = ["is blocked by", "relates to", "is caused by"]

def issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager)
def allLinkTypes = issueLinkTypeManager.getIssueLinkTypes(false)

// Get the outward link names you want
def outwardAllowedLinks = allLinkTypes.findAll { linkType ->
linkType.outward in allowedOutwardTypesNames
}.collectEntries { linkType ->
[(linkType.outward): linkType.outward]
}
// Get the inward link names you want
def inwardAllowedLinks = allLinkTypes.findAll { linkType ->
linkType.inward in allowedInwardTypesNames
}.collectEntries { linkType ->
[(linkType.inward): linkType.inward]
}

// Combine maps of allowed link direction names
def allowedLinks = outwardAllowedLinks + inwardAllowedLinks
log.debug("Allowed Links = $allowedLinks")

linkTypesField.setFieldOptions(allowedLinks as Map<String,String>)

 

I also only check when creating an issue or when editing the issue, not from the issue view screen. Is it because this script can only exclude a link type both inward and outward directions, and not specifically excluding an direction link name?

Edit: Also, i saved this script under Initializer and mapping it to all the issue types except the 1 issue that can use the "has MVP"

PD Sheehan
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.
February 9, 2023

Yes, scriptrunner/behaviours is really only designed to work with user-defined issue screens like Create, Edit or workflow Transition screens.

It has not been extended to work on popups like Move, Link, Clone. 

If the debug log shows the expected list of allowed links, but if the list is not changing in the live screen, then perhaps it's a mapping issue.

You can review the network calls from your browser developer tools and look for calls .../rest/scriptrunner/behaviours/latest/validators.json when you first open the screen. 

It should contain the actions that are expected based on your server-side script execution.

One thing to try is to set a single mapping for your project and all issue types (remove your other mapping to make sure this works. And you can add some filters in the code itself. For example, near the top, add 

if(issueCOntext.issueType.name == 'type that allows has MVP') return
Diana
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.
February 9, 2023

@PD Sheehan 

Yes! That did the trick.

I set the mapping to all issue types, and then in the initialiser script i used

log.debug("issueContext name = $issueContext.issueType.name")

to help track if i'm mapping correctly the issue types, and I used your additional code at the beginning. And now it works.

Thank you!

Suggest an answer

Log in or Sign up to answer