Forums

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

Help with a subtracting a number from a scripted field

Scott Federman
Contributor
May 9, 2018

I currently have a (script-runner) scripted field in an "Order Request" called "Total Order Expense($)" which calculates the sum of all linked "line item orders".  

I have added an RMA issue type which is also linked to the "Order Request" which is how we track returned items. This has a calculated field called "Return Amount" which needs to be subtracted from the "Total Order Expense ($) field in the order request. 

Below is the script i use to calculate the "Total Order Expense ($) now. How would I include the ability to subtract the "Return Amount" from that number?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def csExpense = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Expense")
return issue.getSubTaskObjects().sum{it ->((Issue) it).getCustomFieldValue(csExpense)};

2 answers

1 vote
Brittany Wispell
Community Champion
May 9, 2018

Hey @Scott Federman

Here are some examples of multiplying, adding, subtracting and dividing using groovy. 

You'll need to define each custom field you need. See below example. Let me know if it helps. 

def customfieldnameCF = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Custom Field Name")
def customfieldvalueCFVal = issue.getCustomFieldValue(customfieldnameCF) as Double

def calc = ((customfieldvalueCFVal*3) - customfieldvalueCFVal + customfieldvalueCFVal)/8

def calcc = customfieldvalue1CFVal - customfieldvalue2CFVal - customfieldvalue3CFVal

Scott Federman
Contributor
May 9, 2018

Yup sort of. Pardon my ignorance. But which custom field names do I put where?

Brittany Wispell
Community Champion
May 9, 2018

I'm here to help! 

Depending on where and how you're returning the value for the Expense, it should work like the below. Might need some tweaking, I wasn't able to test it to make sure it works. 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def csExpense = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Expense")
return issue.getSubTaskObjects().sum{it ->((Issue) it).getCustomFieldValue(csExpense)}

def returnamountCF = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Return Amount")
def returnamountCFVal = issue.getCustomFieldValue(returnamountCF) as Double

def calc = returnamountCFVal - csExpenseCFVal

return calc
Scott Federman
Contributor
May 9, 2018

Excellent! I tried it out and its only returning the "Expense" value from the line item issue, but it is not subtracting the "Return Amount" from the RMA issue. 

it should be : Total Order Expense $ (Order Request Issue Type) = Expense (line Item Order Issue Type) - Return Amount (if available) (RMA Issue Type)

Brittany Wispell
Community Champion
May 10, 2018

Is the return amount is in another issue type? 

Scott Federman
Contributor
May 10, 2018

yes it is

Issue type                        Field

Order Request                 Total Order Expense $

Line Item Order               Expense

RMA                                 Return Amount

Deleted user May 14, 2018

@Brittany Wispell I tried monkeying with the script a bit and still no luck:(

Brittany Wispell
Community Champion
May 14, 2018

Hey @Scott Federman 

I've got a couple of SR experts helping with this. Since the custom fields are in different issue types it's a little more complex than I was expecting. I will try to get back to you tomorrow! Sorry for how long it's taking.

0 votes
Scott Federman
Contributor
May 9, 2018

*

Suggest an answer

Log in or Sign up to answer