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

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

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

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) 

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

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
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

229 views 1 2
Join discussion

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