Hello!
I have custom field 10508 and 10509. Each of them could be the numbers from 1 to 10.
The idea is to show calculated sum in the another custom field. As far as I know, I can reach this by adding a script in post function, so the sum will be calculated only after transaction of the issue, is it right? How this field can be updated on fly?
I tried this script, but it doesn't work:
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
def issueManager = ComponentAccessor.getIssueManager()
def linkManager = ComponentAccessor.getIssueLinkManager()
def cfm = ComponentAccessor.getCustomFieldManager()
Issue issueKey = issue
def id=issueKey.getId()
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def cf = customFieldManager.getCustomFieldObject(10508)
def cff = customFieldManager.getCustomFieldObject(10509)
def n = Integer.parseInt((issue.getCustomFieldValue(cf) ?: 0).toString().replaceAll(~/[.].*/, ""))
def b = Integer.parseInt((issue.getCustomFieldValue(cff) ?: 0).toString().replaceAll(~/[.].*/, ""))
total = (sum = (n ?: 0) + (b ?: 0))
return total
Could you help me?
If you want the "calculated" field to be always up to date (and not just updated during transitions), you should create a scripted field. This can be done in ScriptRunner or using the JMCF app (which makes it a little easier thanks to a simplified API).
Thanks to community.
Done by creating scripted field:
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(10508)
def Rec = customFieldManager.getCustomFieldObject(10509)
def Cus = customFieldManager.getCustomFieldObject(10510)
def Acc = customFieldManager.getCustomFieldObject(10511)
def Ret = customFieldManager.getCustomFieldObject(10512)
def a = Integer.parseInt((issue.getCustomFieldValue(BI) ?: 0).toString().replaceAll(~/[.].*/, ""))
def b = Integer.parseInt((issue.getCustomFieldValue(Rec) ?: 0).toString().replaceAll(~/[.].*/, ""))
def c = Integer.parseInt((issue.getCustomFieldValue(Cus) ?: 0).toString().replaceAll(~/[.].*/, ""))
def d = Integer.parseInt((issue.getCustomFieldValue(Acc) ?: 0).toString().replaceAll(~/[.].*/, ""))
def e = Integer.parseInt((issue.getCustomFieldValue(Ret) ?: 0).toString().replaceAll(~/[.].*/, ""))
def sum = (a ?: 0) + (b ?: 0) + (c ?: 0) + (d ?: 0) + (e ?: 0)
return sum
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Николай Киселев ,
Your code seems fine and you could update a field adding this snippet :
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
def fieldToUpdate = customFieldManager.getCustomFieldObject(10510)
def oldValue = issue.getCustomFieldValue(fieldToUpdate)
fieldToUpdate.updateValue(null, issue, new ModifiedValue(oldValue, total), new DefaultIssueChangeHolder())
Keep the reindex post-function after the script.
Antoine
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.