Groovy script compare to fields with subtraction

Jesper Mandrup Keld October 23, 2019

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
fran garcia gomera
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.
October 24, 2019

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";
Jesper Mandrup Keld October 24, 2019

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

fran garcia gomera
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.
October 28, 2019

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.

0 votes
Answer accepted
Arthur SALMON October 23, 2019

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";
Jesper Mandrup Keld October 24, 2019

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

Arthur SALMON October 24, 2019

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. 

Jesper Mandrup Keld October 24, 2019

Still get the same error @Arthur SALMON 

Arthur SALMON October 24, 2019

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" ?

Jesper Mandrup Keld October 24, 2019

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

Arthur SALMON October 24, 2019

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.

Jesper Mandrup Keld October 25, 2019

Thanks for trying @Arthur SALMON 

Suggest an answer

Log in or Sign up to answer