Forums

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

Scriptrunner Scripted Field Help: Add Values of Two Number Fields, divide result, and multiply

David
Contributor
November 4, 2019

Hello,
Looking for help in a scripted field for scriptrunner.

I have two number fields:
Incorrect
Correct

In the scripted field, I'm trying to do this If/Else equation:
IF:
Correct && Incorrect do not equal zero:
return the number from this equation:
Correct/(Incorrect + Correct) * 100

IF:
Incorrect equals zero && Correct does not equal 0:
return the number 100

Else:
return the number 0

1 answer

1 accepted

2 votes
Answer accepted
PD Sheehan
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.
November 4, 2019

Here is something to get you started

import com.atlassian.jira.component.ComponentAccessor 
def customFieldManager = ComponentAccessor.customFieldManager

def correctField = customFieldManager.getCustomFieldObjectsByName('Correct')[0]
def incorrectField = customFieldManager.getCustomFieldObjectsByName('Incorrect')[0]
if(!correctField || !incorrectField){
log.error "Can't evaluate script field '$name'. Missing custom field 'Correct' or 'Incorrect'"
return 0
}
def correct = issue.getCustomFieldValue(correctField)?.toDouble() ?: 0
def incorrect = issue.getCustomFieldValue(incorrectField)?.toDouble() ?: 0

if(correct && incorrect){
correct/(incorrect + correct) * 100
} else if (correct && !incorrect) {
100
} else {
0
}

This assumes that both fields are correctly configured as number fields. Otherwise, the toDouble() method will fail.

The "?: 0" mean if there is no value for the custom field, assume a value of 0.

I'll point out that the second condition is equivalent to the first one. For example, 2 correct and 0 incorrect 2/(2+0)*100 = 100. So no real need for that condition. So this would work too:

if(correct ){
   correct/(incorrect + correct) * 100
} else {
0
}

Note that in groovy, "thrutiness" for number means that a condition will be false if the number is zero or null. Anything else is true. But if it makes it easier for you, you could write if(correct >0) 

David
Contributor
November 5, 2019

Thanks for the solid response and help Peter,

I ended up figuring out the below code which is working for me, but I plan on implementing some of your code as modifications/improvements. Particularly the "?:0", that's really helpful.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.opensymphony.workflow.WorkflowContext

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def incorrectCf = customFieldManager.getCustomFieldObjectsByName("Incorrect")
def correctCf = customFieldManager.getCustomFieldObjectsByName("Correct")


def incorrect = issue.getCustomFieldValue(incorrectCf) as Integer
def correct = issue.getCustomFieldValue(correctCf) as Integer

if (correct != 0 && incorrect != 0){
return correct/(correct + incorrect)*100 as Integer
}
if (correct != 0 && incorrect == 0){
return "100"
}
else{
// return to some code to indicate a null value in one of the fields
return "0"
}
PD Sheehan
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.
November 5, 2019

Glad to help.

Just be careful with getCustomFieldObjectsByName and getCustomFieldObjectByName (one has objectS). The singular one is deprecated as of Jira 8 and the other will return an array of customfields. So you may need to look for item 0 in the list.

Or else, use getCustomFieldObject(Long customFieldId) to be absolutely certain you are getting the correct field.

Suggest an answer

Log in or Sign up to answer