jira scripted field to divide two fields (Scripted number field by custom number field)

Amit Kumar Singh April 17, 2023

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean

// Get values from 2 custom fields
Double val1 = makeNullZero(getCustomFieldValue('Total Annual $ Amount (Run Rate)'));
Double val2 = makeNullZero(getCustomFieldValue('Estimated Development Cost (One-Time) ($)'));
Double Cost_Ratio = (val1 / val2) ;

return Cost_Ratio;

static Double makeNullZero(Double val) {
return null == val ? 0 : val;
}

 

Jira Scripted Field Error.PNG

2 answers

1 accepted

0 votes
Answer accepted
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 18, 2023

Looks to me like you might have tried to use ChatGPT to write a script here - it's well known to make up functions that simply aren't there, like "makeNullZero".

Try this:

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager

def n1 =  customFieldManager.getCustomFieldObjectsByName('field name 1').first()
def n2 =  customFieldManager.getCustomFieldObjectsByName('field name 2').first()

if (n1 && n2) {
  return (n1/n2)
}

return 0
Amit Kumar Singh April 18, 2023

@Nic Brough -Adaptavist-  getting below error on return code...

I appreciate your effort on this.... need more help on this... 

Jira Scripted Field Error2.PNG

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 18, 2023

I am sorry, I left out the bit in the middle!  (Too much coffee and rushing my answers is not good)

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager

def n1f =  customFieldManager.getCustomFieldObjectsByName('field name 1').first()
def n2f =  customFieldManager.getCustomFieldObjectsByName('field name 2').first()

def n1 = issue.getCustomFieldValue(n1f)
def n2 = issue.getCustomFieldValue(n2f)

if (n1 && n2) {
  return (n1/n2)
}

return 0

 Also, if you have a recent version of Scriptrunner for Server/DC, you can do this faster and easier with HAPI functions (these work alongside existing scripts, you can think of HAPI as doing a lot of the basic work for you - this script doesn't need imports or fetching custom field objects):

 

def n1 = issue.getCustomFieldValue('field name 1')
def n2 = issue.getCustomFieldValue('field name 2')

if (n1 && n2) {
  return (n1/n2)
}

return 0
Amit Kumar Singh April 18, 2023

@Nic Brough -Adaptavist- declared type for return needs some update, i don't know what should be, but still getting error.

Jira Scripted Field Error2.PNG

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 19, 2023

That suggests that one, or both, of the fields are not numeric fields.

Amit Kumar Singh April 19, 2023

Total Annual $ Amount (Run Rate) - Scripted Field (Number Field - A floating point number)

Estimated Development Cost (One-Time) ($) - Number Field

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 19, 2023

Ah.  The output of a scripted field might look like it is in a certain format, but a) it might not be, and b) you can't rely on it to be accurate.

You need to swap the read of the scripted field to a read of a non-scripted numeric field (or if it's not numeric, cast the value to a number before calculating from it)

Amit Kumar Singh April 19, 2023

I have no idea how to cast or convert it in this script, could please give a last try and help me to get this code working....

Amit Kumar Singh April 24, 2023

@Nic Brough -Adaptavist- 

Below code is working fine, but need result limited to two decimal placeses.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean

// Get values from 4 custom fields
Long val1 = makeNullZero(getCustomFieldValue('Benefit 1 Annual $ Amount (Run Rate) ($)'));
Long val2 = makeNullZero(getCustomFieldValue('Benefit 2 Annual $ Amount (Run Rate) ($)'));
Long val3 = makeNullZero(getCustomFieldValue('Benefit 3 Annual $ Amount (Run Rate) ($)'));
Long val4 = makeNullZero(getCustomFieldValue('Benefit 4 Annual $ Amount (Run Rate) ($)'));
Long val5 = makeNullZero(getCustomFieldValue('Estimated Development Cost (One-Time) ($)'));

BigDecimal sum = (val1 + val2 + val3 + val4) / val5;
return sum;

static Double makeNullZero(Double val) {
return null == val ? 0 : val;
}

if i do not use "BigDecimal" it is throwing error.

****  Estimated Benefit to Cost Ratio:  2.667    *******

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 24, 2023

I'm not a Groovy coder, I don't have memories of all the Groovy core code here, but if you're happy to drop down into the underlying Java, try 

return (double) Math.round(sum * 100) / 100
Amit Kumar Singh April 25, 2023

@Nic Brough -Adaptavist-  Thank You!

Below code is working fine now...

But *** sum = (val1 + val2 + val3 + val4)**** Is not taking decimal values...

sum = (2.5+ 2.5 + 2.5 + 2.5)  =  8, it should be 10

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean

// Get values from 4 custom fields
Long val1 = makeNullZero(getCustomFieldValue('Benefit 1 Annual $ Amount (Run Rate) ($)'));
Long val2 = makeNullZero(getCustomFieldValue('Benefit 2 Annual $ Amount (Run Rate) ($)'));
Long val3 = makeNullZero(getCustomFieldValue('Benefit 3 Annual $ Amount (Run Rate) ($)'));
Long val4 = makeNullZero(getCustomFieldValue('Benefit 4 Annual $ Amount (Run Rate) ($)'));
Long val5 = makeNullZero(getCustomFieldValue('Estimated Development Cost (One-Time) ($)'));

BigDecimal sum = (val1 + val2 + val3 + val4) / val5
return sum.round(2);

static Double makeNullZero(Double val) {
return null == val ? 0 : val;
}

0 votes
Amit Kumar Singh April 17, 2023

.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events