Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

Groovy script for field comparison

Hi all, I'm looking for help for a groovy script, which is not what I'm best at :)

In a transition from A to B, I would like to compare two fields:

Vendor: Insight Referenced Object (single) / Custom field

Product: Select List (single choice) / Custom field

The simple scripted validator should check, if Product is filled out, if Vendor equals certain values ("Supplier (A)" or "Supplier (B)"). If Vendor has any other value than these two, Product doesn't have to be filled out.

I have tried with various examples of this script:



import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager

def cfVendor = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10806").toString()
def value1 = issue.getCustomFieldValue(cfVendor)?:"" as String

def cfProduct = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_14302").toString()
def value2 = issue.getCustomFieldValue(cfProduct)?:"" as String

if(value1 == "Supplier (A)" && value2 == null)
return false

1 answer

0 votes

It's almost never a good idea to coerce an object (like custom field object or insight object bean object) to a string. 

Better find the correct method for that object to get the desired info.

Here is a script that should work for you

import com.onresolve.scriptrunner.runner.customisers.WithPlugin

//this allows importing ObjectBean above, and helps with autocomplete and static type checking
@WithPlugin('com.riadalabs.jira.plugins.insight') insightPlugin

def vendorObjects = cfValues['Vendor']
if(!vendorObjects) return true //nothing to validate if vendor is empty. Return false, if the field is required, or mark is required in another validator to have a different message

//jira always returns a list even when multiple is not allowed, so assume we want the first and only value in the list
def vendorObject = vendorObjects.first() as ObjectBean

def productRequiredVendors = ['Supplier (A)', 'Supplied (B)'] //don't include the key, just the display label.
if(!productRequiredVendors.contains(vendorObject.label)) return true //don't validate the product, the current vendor is not in the list

//if we're still running (no return true encountered), check the product
def product = cfValues['Product']

return (product) //this will be true if product is filled in or false if it wasn't

Note that cfValues is only available in simple scripted validators and simple scripted condition (perhaps also in some postfunctions). But not in custom script validators and conditions. IN those, then you need to use customFieldManager. But not here. 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Apps & Integrations

Marketplace Partner Spotlight: AppLiger

This month the spotlight is on AppLiger. We caught up with Pavel Pavlovsky, CEO and Product Manager, to learn how the company started and what fuels the team's creativity.    Atlassian:...

393 views 0 7
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you