It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Groovy script compare to fields with subtraction

I'm trying to compare to numbered fields and want to throw a specific text when the one field are 2 numbers or less away from the other field. I tried with a scripted field to do below but it will not accept the minus i have after field 2

-------------------

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def Field1 = customFieldManager.getCustomFieldObject(00001);
def Field2 = customFieldManager.getCustomFieldObject(00002);

def TFTR = issue.getCustomFieldValue(Field1);
def EH = issue.getCustomFieldValue(Field2);

if (EH <= TFTR - 2) return "Some text 1";
else return "Some text 2";

--------------------

Hope someone can help

2 answers

2 accepted

0 votes
Answer accepted

Hi, 

Do you have anything more specific about the error?

I think you could try converting your value to integer this way : 

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def Field1 = customFieldManager.getCustomFieldObject(00001);
def Field2 = customFieldManager.getCustomFieldObject(00002);

def TFTR = issue.getCustomFieldValue(Field1).toInteger();
def EH = issue.getCustomFieldValue(Field2).toInteger();

if (EH <= (TFTR - 2)) return "Some text 1";
else return "Some text 2";

Thanks for chipping in @Arthur SALMON i just tried the convert to integer that you sugested, but without success. The error i get from script is below - hop you will be able to assist based on that. 

2019-10-24_15-33-03.png

I think this code should do the trick :

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def Field1 = customFieldManager.getCustomFieldObject("customfield_00001");
def Field2 = customFieldManager.getCustomFieldObject("customfield_00002");

def TFTR = (int)issue.getCustomFieldValue(Field1);
def EH = (int)issue.getCustomFieldValue(Field2);

if (EH <= (TFTR-2)) return "Some text 1";
else return "Some text 2";

Just replace the 00001 & 00002 with the id of your custom field. You can find it when you click on "configure" on the custom field in the url address. 

Still get the same error @Arthur SALMON 

I tested it and it worked fine for me, that is weird. Did you set the (int) in your def TFTR & def EH ?

Both your field are "number fields" ?

Both fields are numbered fields but "field1" is also scripted from an external DB 

It could be the reason why.

You should try to set up logs or check directly on Jira if you have more specific errors. I can't help on that, I haven't done it yet.

0 votes
Answer accepted

This should work, obviously you have to adapt the value you want in case the fields are null

def TFTR
def EH

if (issue.getCustomFieldValue(Field1) !=null) TFTR = (int) issue.getCustomFieldValue(Field1);
else TFTR = 0;
if (issue.getCustomFieldValue(Field2) !=null) EH = (int) issue.getCustomFieldValue(Field2);
else EH = 0;

if (EH <= (TFTR - 2)) return "Some text 1";
else return "Some text 2";

Thanks @fran garcia gomera now i have no errors in the script but it just returns null - no text - any ideas?

which are the values of the fields in the issues you are trying?

the final if/else should always return a text unless there is an error before that returns the null. Try adding logs to see the values of the variables before the if and to check if they go into the if/else properly.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Jira

The add-in you’ve been waiting for: Jira Cloud for Excel 🙌

Introducing Jira Cloud for Excel Here at the product integrations team at Atlassian, we are thrilled to announce the new Jira Cloud for Excel add-in! This add-in lets you export Jira data directly ...

872 views 6 24
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