Groovy script for field comparison

Thomas Biehl June 15, 2022

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
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.
June 25, 2022

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
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean

//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
TAGS
AUG Leaders

Atlassian Community Events