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;
}
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
@Nic Brough -Adaptavist- getting below error on return code...
I appreciate your effort on this.... need more help on this...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Nic Brough -Adaptavist- declared type for return needs some update, i don't know what should be, but still getting error.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That suggests that one, or both, of the fields are not numeric fields.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Total Annual $ Amount (Run Rate) - Scripted Field (Number Field - A floating point number)
Estimated Development Cost (One-Time) ($) - Number Field
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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....
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 *******
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.