Forums

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

I want to sum up the previous value and the current update value of a custom field and display the r

Ashok Barik
Contributor
March 25, 2022
I want to sum up the previous value and the current update value of a custom field and display the result in another field. 
For example:
I have two fields: field1 (Money spent) and field2 (Total spent). If I add a number in  field1 then it should sum up with the previous value of it and then display the result in the Total spent field. 
Details:
Jira Software - 8.4.3
ScriptRunner available = yes

1 answer

0 votes
Martin Bayer [MoroSystems, s.r.o.]
Community Champion
March 26, 2022

Hi @Ashok Barik is it ok to set the "Money spent" value only via transition screen or you need to edit it via inline edit on issue detail?

In case you can do it via transition screen (I suggest this solution), you can use scripted postfunction. In case you need to do it via inline edit, you need to use Listener which listens to Issue Updated event.

I think you can use following logic

  • on the beginning (after issue is created)
    • Money spent = null
    • Total spent = null
  • on first edit of Money spent field (it is set to 5)
    • Money spent set to 5
    • Total spent = Total spent + 5
  • on second edit of Money spent field (it is set to 10)
    • Money spent set to 10
    • Total spent = Total spent + 10

Let me know, if you need more information

Ashok Barik
Contributor
March 26, 2022

Hi @Martin Bayer [MoroSystems, s.r.o.] I need to edit it via inline edit on issue detail. 

I used a field script in the script runner, but the value for the Total spent field was incorrect.  Could you please send me the script for this, if it is possible in Listener?   

Martin Bayer [MoroSystems, s.r.o.]
Community Champion
March 26, 2022

Hi @Ashok Barik sure, more information about Listeners are here:

https://docs.adaptavist.com/sr4js/latest/features/listeners/custom-listener

Adaptavist also prepared some code snippets and there is one with listener...

Let me know if you need more help

Ashok Barik
Contributor
March 26, 2022

Hi @Martin Bayer [MoroSystems, s.r.o.] Thanks a lot for providing me with all details. 

 

I used this script in the Script Runner field but it didn't work, can it work in 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

Issue issueKey = issue
def id=issueKey.getId()
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def cff = customFieldManager.getCustomFieldObjectByName("Money Spent")
def b = Integer.parseInt((issue.getCustomFieldValue(cff) ?: 0).toString().replaceAll(~/[.].*/, ""))

def changeHistoryManager = com.atlassian.jira.component.ComponentAccessor.getChangeHistoryManager()
def old = changeHistoryManager.getChangeItemsForField(issue, 'Total Spent')
def d = 0;
if (old != null) {
Integer sum = b + d
return sum
}
else {
def cf = customFieldManager.getCustomFieldObjectByName("Total Spent")
def n = Integer.parseInt((issue.getCustomFieldValue(cf) ?: 0).toString().replaceAll(~/[.].*/, ""))
Integer sum = n + b

}

Ashok Barik
Contributor
March 26, 2022

Hi @Martin Bayer [MoroSystems, s.r.o.] 

I am new to the script runner and am not a programmer. It would be greatly appreciated if you could provide me with the script. 
I appreciate your valuable time and all your support. 
Martin Bayer [MoroSystems, s.r.o.]
Community Champion
March 29, 2022

Hi @Ashok Barik , followint script is working for me :)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

def issue = event.issue

def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def change = event.changeLog.getRelated("ChildChangeItem")?.find { it.field == "Money spent" }

// a field other than the Account field updated, nothing to do here
if (!change) {
return
}

def customFieldManager = ComponentAccessor.customFieldManager
def moneySpentField = customFieldManager.getCustomFieldObjects(issue).find { it.name == "Money spent" }
def totalSpentField = customFieldManager.getCustomFieldObjects(issue).find { it.name == "Total spent" }

def moneySpent = issue.getCustomFieldValue(moneySpentField) as Double
moneySpent = moneySpent ?: 0

def totalSpent = issue.getCustomFieldValue(totalSpentField) as Double
totalSpent = totalSpent ?: 0

totalSpentField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(totalSpentField), totalSpent + moneySpent), new DefaultIssueChangeHolder())

Suggest an answer

Log in or Sign up to answer