Are you in the loop? Keep up with the latest by making sure you're subscribed to Community Announcements. Just click Watch and select Articles.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

ScriptRunner Field Behaviours Change Select Options for Asset Fields

I'm aware that this is potentially achievable using asset custom fields and reference asset custom fields, however I have a unique circumstance that extends beyond this.

Context:

I have an asset model that has four object types:

  • A
  • B (with references to A)
  • C (with references to A)
  • D (with references to A)

I have exposed all these object types through custom fields like so:

  • A (asset field)
  • B (reference asset field) referenced to A
  • C (reference asset field) referenced to A
  • D (reference asset field) referenced to A.

Using ScriptRunner, I want to define a field behaviour whereby options for customfield D (which is object type D) only displays objects D1, D2 and D3 if B1 in customfield B has been selected. Otherwise, I only want D1 and D2 to be selectable. Any assistance with the groovy aspect of this would be much appreciated. I've been able to interact with assets previously using the ObjectFacade, however setting the field options is proving to be slightly more challenging.

2 answers

1 vote
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Oct 17, 2023

Hi @Amos

It's a bit of a tricky requirement. You won't be able to filter the Asset field using the Behaviour. This must be done using the Field's configuration and adding the IQL query.

However, for the Behaviour, you must create 2 separate Server-Side Behaviours, i.e. for both the Asset fields separately and with that you will need to use the setErrorMessage to trigger the error when a wrong option is selected.

Below are two sample codes for your reference: (it's not 100% tested)

import com.adaptavist.hapi.jira.assets.Assets
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def asset = getFieldById(fieldChanged)
def assetValue = asset.value.toString()

def subAsset = getFieldByName('Sub Asset')
def subAssetValue = subAsset.value.toString()
subAsset.clearError()

if (assetValue in ['SA-1', 'SA-8']) {
def filter = Assets.search(""""Host" IN (${assetValue})""" ).collect() as List<ObjectBean>
def filterNames = filter.objectKey as List<String>

if (subAssetValue.length() > 0) {
if (subAssetValue.contains(',')) {
def subAssetFilter = subAssetValue.replace('[','').replace(']','').trim().split(',') as List
if (!filterNames.intersect(subAssetFilter)) {
subAsset.setError('Invalid Sub-Asset Selected')
}
} else {
if (!filterNames.contains(subAssetValue)) {
subAsset.setError('Invalid Sub-Asset Selected')
}
}
}
}

and

import com.adaptavist.hapi.jira.assets.Assets
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def asset = getFieldByName('Assets')
def assetValue = asset.value

def subAsset = getFieldById(fieldChanged)
def subAssetValue = subAsset.value.toString()
subAsset.clearError()

if (assetValue in ['SA-1', 'SA-8']) {
def filter = Assets.search(""""Host" IN (${assetValue})""" ).collect() as List<ObjectBean>
def filterNames = filter.objectKey as List<String>

if (subAssetValue.length() > 0) {
if (subAssetValue.contains(',')) {
def subAssetFilter = subAssetValue.replace('[','').replace(']','').trim().split(',') as List
if (!filterNames.intersect(subAssetFilter)) {
subAsset.setError('Invalid Sub-Asset Selected')
}
} else {
if (!filterNames.contains(subAssetValue)) {
subAsset.setError('Invalid Sub-Asset Selected')
}
}
}
}

The first one is for Custom Field B and the second is for Custom Field D.

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

Thank you and Kind regards,

Ram

Thanks Ram, I'm most certainly going to give this a go.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Oct 25, 2023

Hi @Amos

Was the solution able to help solve your question?

Thank you and Kind regards,

Ram

1 vote
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Oct 17, 2023

Due to the dynamic nature that the Asset Custome field load the available options, Behaviours is not going to be able to interfere.

The only thing you can do with Behaviours is to intercept the user's selection, place the form in an error state, and provide feedback to the user that the selection is invalid. You can't limit the selection itself.

But that should be possible within the Issue Scope IQL in the custom field configuration.

Thanks Peter, we've been trying to use the Filter Issue Scope (AQL) to do this, but with very limited success. I created an outbound reference between the objects I wanted the behaviour to occur with. I then tried:

object HAVING outboundReferences(Label IN (${customfield_xxxxx}))

There error we get is <iql,Placeholder not support in current context> but this is not a very useful error message, so we're not having much luck on this front. It seems like the simple and obvious answer.

My take on this, having just done a few hours of looking around is Filter Issue Scope (AQL) doesn't support custom fields that are asset or reference asset object fields. If it does, then the documentation leaves a lot left to be desired.

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Oct 23, 2023

Not at all, I have several Asset Custom fields with complex cascading dependencies. Enough to give you a headache trying to understand what it does:

(Customer IN (${Customer(s)${0}}) OR object having outR(objectType = Customer AND object having inR(objectType = "Customer" and Key IN (${Customer(s)${0}}),referenceType in ("Has")))) AND "Issue Types" like ${issuetype.name} AND "Environment Type" IN (${Environment Type${0}})

In my example "Customer(s)" and "Environment Type" are both Asset fields. 

 

It's a bit difficult to consult in the abstract, if you could attach some concrete examples with screenshots, I'm sure I can help you come up with an Issue Scope IQL

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
9.4.10
TAGS
AUG Leaders

Atlassian Community Events