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