Forums

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

how to calculate the sum of 2 custom fields to the third custom field

Andrews September 3, 2020

Hi,

I want to display sum of two number fields to third number field.

ex: 1 Support Count CRM(2.0) = 510

      2 Support Count CRM(Old) = 10

      3 Support Count CRM(New) = 4

 

Calculate the sum of 2 custom fields (2+3) to the third custom field (1)

 

  • 1= 2+3 
  • 510 = 10+4
  • 524

2 answers

0 votes
Andrews September 3, 2020

@Mathis Hellensberg - Where do i add the script apart from the script field??

Mathis Hellensberg
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 3, 2020

First you create the script field and place it on whatever screen you need it on, then go to manage apps and you'll be able to find it under Scriptrunner > Fields :)

0 votes
Mathis Hellensberg
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 3, 2020

Assuming they are all 'Number Fields' this approach should work.

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue

def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def issue = issueManager.getIssueObject("ITSD-57")

def number1Value = customFieldManager.getCustomFieldObjectsByName("Number 1")[0].getValue(issue)
def number2Value = customFieldManager.getCustomFieldObjectsByName("Number 2")[0].getValue(issue)
def number3Field = customFieldManager.getCustomFieldObjectsByName("Number 3")[0]
def number3Value = number1Value + number2Value

number3Field.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(number3Field), number3Value), new DefaultIssueChangeHolder())
Andrews September 3, 2020 edited

@Mathis Hellensberg - So again do i need to create any script field?

if script field not required then where do i update the script?

Mathis Hellensberg
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 3, 2020 edited

You could very well do it in a scriptet field :)

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager

def number1Value = customFieldManager.getCustomFieldObjectsByName("Number 1")[0].getValue(issue)
def number2Value = customFieldManager.getCustomFieldObjectsByName("Number 2")[0].getValue(issue)

number1Value + number2Value
   

Suggest an answer

Log in or Sign up to answer