I'm new to Jira and I'm trying to calculate the sum of a custom field with the result field (scripted) and want to display the sum result on the same result field (scripted).
Example:
I have custom fields: field_1 (Expenses) and another script runner field field_2 (Total Expenses).
If I add a number in field_1 then it should sum up with the field_2 (Total Expenses) value and then display the result in the same field_2.
I have looked over the ScriptRunner library and various Snippets, but I'm still not getting the expected result.
When I create a new Issue and do not put any number in Field_1, then result shows in Field_1 -> 0 and Field_2 -> 0 which is correct, but after when I enter a number in Field_1, the result field Field_2 (Total Expenses) displays some random number.
I would be extremely thankful if someone could point out where I'm going wrong.
I am using the script in the ScriptRunner field and here is my script:
--------------------
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
def issueManager = ComponentAccessor.getIssueManager()
def linkManager = ComponentAccessor.getIssueLinkManager()
def cfm = ComponentAccessor.getCustomFieldManager()
Issue issue = issue
def id=issue.getId()
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def BI = customFieldManager.getCustomFieldObject(18844)
def Rec = customFieldManager.getCustomFieldObject(18754)
def a = Integer.parseInt((issue.getCustomFieldValue(BI) ?: 0).toString().replaceAll(~/[.].*/, ""))
def b = Integer.parseInt((issue.getCustomFieldValue(Rec) ?: 0).toString().replaceAll(~/[.].*/, ""))
def sum = (a ?: 0) + (b ?: 0)
return sum
-------------------
18844 - (Field_1)
18754 - (Field_2) Total Expenses
Jira software version - 8.4.3
Hi @Ashok Barik
For your requirement, you could try something like this:-
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.customFieldManager
def number1 = customFieldManager.getCustomFieldObjectsByName('Sample Number 1').first()
def number1Value = number1.getValue(issue) as Long
def number2 = customFieldManager.getCustomFieldObjectsByName('Sample Number 2').first()
def number2Value = number2.getValue(issue) as Long
(number1Value + number2Value) as Long
Please note, the working sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.
Instead of converting the value of the Number field to a String and parsing to a Numeric type, you can use the as Long option, i.e.
def number1Value = number1.getValue(issue) as Long
and
def number2Value = number2.getValue(issue) as Long
as shown in the sample code above. This will implicitly remove the decimal points.
Also, you must ensure that when you have done the calculation, the total must also be converted to a Long type as shown in the code above, i.e.
(number1Value + number2Value) as Long
Below is a print screen on of the Scripted Field config:-
Below are a few test print screens for your reference:-
1. When the issue is being created, the Sample Number 1 and Sample Number 2 numeric fields are filled up.
2. Upon creating the ticket, the Scripted Field, Total Value is displayed with the total of the Sample Number 1 and Sample Number 2 fields.
I hope this helps to solve your question. :)
Thank you and Kind regards,
Ram
Hi @Ram Kumar Aravindakshan _Adaptavist_ Thank you for your efforts, and btw, I was able to fulfill my requirement by implementing a script in the scriptrunner listener.
In my request, I asked for:
1. Create an issue > field1 is blank > field2 shows "0".
2. Edit the created issue > Enter "5" into field1 > field2 should display: "5" (5 + 0).
3. Edit the same issue again > Enter "14" into field 1 > field2 should display: "19" (5 + 14).
I created another numeric custom field for Total Expenses and made it readonly.
and here is my script for Listener.
----------------------------------------------
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user.ApplicationUser
// ========== Added to update the custom field value of an issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
Issue issue = event.issue // ========== Get the event issue
def id=issue.getId()
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def cff = customFieldManager.getCustomFieldObject("Expenses") //This is field1 and it is editable.
def b = Integer.parseInt((issue.getCustomFieldValue(cff) ?: 0).toString().replaceAll(~/[.].*/, ""))
def cf = customFieldManager.getCustomFieldObjectsByName("Total Expenses")[0] //Field2 non editable.
def n = Integer.parseInt((issue.getCustomFieldValue(cf) ?: 0).toString().replaceAll(~/[.].*/, ""))
def changeHistoryManager = com.atlassian.jira.component.ComponentAccessor.getChangeHistoryManager()
// ========== Get this change log of the field1 from the triggered event
def old = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "Expenses"} // This is field1
def d = 0;
Double sum = 0 // ========== Define the "sum" as double so it can be stored into the Number field (field2)
// ========== If event type id is 1 which is CREATE ISSUE, update the field2 value with field1 value (if blank, 0)
if(event.eventTypeId == 1){
sum = sum + b
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), sum), new DefaultIssueChangeHolder())
}else{ // ========== Else, it's an UPDATE event
if(old){ // ========== If change item is field1
if(old["oldstring"] == null){ // ========== If the field1 previous value is empty/null
sum = b + d // ========== field1 + 0
}else{
sum = n + b // ========== field1 + field2 old value
}
// Update the field2 value with the "sum"
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), sum), new DefaultIssueChangeHolder())
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Ashok Barik
Glad to hear you were able to solve it.
The example I gave you is a more straightforward approach than the Listener.
If you require the 0 value to be visible in the Total field, you can modify your code slightly like:-
def total = (number1Value + number2Value) as Long
if(total.toString().length() == 0) {
0
} else {
total
}
If there is no value added to both the fields, the total will implicitly return 0.
Else, it will return the value of both fields added to either of the fields.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
hi @Ashok Barik
Have I answered this question in https://community.atlassian.com/t5/Jira-questions/When-an-issue-is-edited-via-the-issue-UI-s-inline-edit-the/qaq-p/1984243#M526396 ?
If so can you please accept the answer provided here as well?
Thank you and Kind regards,
Ram
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.