Forums

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

How to compare a field and the lead associated to a component ?

Stéphane Eursels December 10, 2020

Hi,

To validate the condition of a transition I must be able to compare the name of a user entered in an issue field and the lead associated with the selected component in the same issue.

I have found two solution based on ScriptRunner :

The first to compare two field :

cfValues['Field1'] == cfValues['Field2']

The second to find the lead of the first component selected in an issue

package com.onresolve.jira.groovy.test.scriptfields.scripts

def components = issue.componentObjects.toList()
if (components) {
return components?.first()?.componentLead
}

But when i want to merge to compare the component lead and a field it doesn't work.

 

package com.onresolve.jira.groovy.test.scriptfields.scripts

def components = issue.componentObjects.toList()
if (components) {
return components?.first()?.componentLead
}

For example :

package com.onresolve.jira.groovy.test.scriptfields.scripts

def components = issue.componentObjects.toList()
if (components) {
return components?.first()?.componentLead == cfValues['Field1']
}

Can you help me ?

Regards 

1 answer

1 accepted

0 votes
Answer accepted
Nic Brough -Adaptavist-
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.
December 10, 2020

Field == Field only comes out as true when the content of the fields are the same.  The fields you're comparing are of two different types, let alone content, so they'll never compare the same. 

It's a bit like comparing an apple with a kiwi - one aspect of both is that they're green, but your equivalence question is "is this apple the same as a kiwi?". 

What you need to do is extract the aspects that you actually want to compare from each object, so using the fruit in a pseudo-code example, don't ask "apple == kiwi", but "apple.colour == kiwi.colour".

Your code already does that for components, it's getting a user object back from the componentLead call, but it's then trying to compare that user object with the content of a field.  Unless that field also contains a user object, it's not going to work.  So, you might assume that means that it should work if your Field1 is a single user picker (any other field type won't hold a single user as the content)

It probably won't though.  Atlassian wrap select-list field content in singleton arrays in some places and I think they do it with the user picker - the content coming back from cfValues[Field1] will be an array with one element that contains a user, so you'd need to extract that.  On top if that, there are several object types of user, so you may need to come up with another comparison again!

So, I think you might be looking at something more like:

components?.first()?.componentLead.getName() == cfValues['Field1'].first.getName()

if the fields hold different types of user and Field1 is in an array.

You'd need to look at the class definitions for the user objects if you want to compare on something else

Stéphane Eursels December 10, 2020

Many thanks, I have adapted my code as below and it's work...

package com.onresolve.jira.groovy.test.scriptfields.scripts

def components = issue.getComponents()

if (components) {

    if (components?.first()?.componentLead.getName() == cfValues['Field1'].getName()) {

        return true;

    }else{

        return false;

    }

}else{

    return false;

}

Like Nic Brough -Adaptavist- likes this

Suggest an answer

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

Atlassian Community Events