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

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
4,559,698
Community Members
 
Community Events
185
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
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.
Jun 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