Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
Java Script code to sum 6 different field's value

Image for required code to add value in Total field Initially i had 4 filed to sum, but now 2 more fields are added. as per 4 fields i have below code but adding 2 more it is becoming more complex as per my knowledge.

required sum for these 6 fileds

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean

// Get values from 4 custom fields
def val1 =  getCustomFieldValue("FTE US") as Double
def val2 = getCustomFieldValue("FTE OGS IND") as Double
def val3 = getCustomFieldValue("FTE OGS PHL") as Double
def val4 = getCustomFieldValue("FTE Vendor") as Double

// check both fields contain valid numeric values
if (val1 == null && val2 == null && val3 == null && val4 == null){
    return 0
}else if (val1 != null && val2 != null&& val3 != null&& val4 != null){
     return val1 + val2 + val3 + val4
}else if (val1 == null && val2 != null&& val3 != null&& val4 != null){
     return 0 + val2 + val3 + val4
}else if (val2 == null && val1 != null&& val3 != null&& val4 != null){
     return 0 + val1 + val3 + val4
}else if (val3 == null && val1 != null&& val2 != null&& val4 != null){
     return 0 + val1+ val2 + val4
}else if (val4 == null && val1 != null&& val2 != null&& val3 != null){
     return 0 + val1 + val2 + val3
}else if (val3 == null && val4 == null&& val1 != null&& val2 != null){
     return 0 + val1 + val2
}else if (val2 == null && val4 == null&& val1 != null&& val3 != null){
     return 0 + val1 + val3
}else if (val2 == null && val3 == null&& val1 != null&& val4 != null){
     return 0 + val1 + val4
}else if (val1 == null && val4 == null&& val2 != null&& val3 != null){
     return 0 + val2 + val3

}else if (val1 == null && val3 == null&& val2 != null&& val4 != null){
     return 0 + val2 + val4
}else if (val1 == null && val2 == null&& val3 != null&& val4 != null){
     return 0 + val3 + val4
}else{
    // return to some code to indicate a null value in one of the fields
 return "0"   
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Much easier put all the values into an array and then call:

[val1, val2, val3, val4].filter((x) => !isNaN(x)).reduce((a, b) => a + b)

What I'm doing is:

  • use filter to remove "not number" elements (!isNaN(x));
  • use a reducer to sum all the remaining elements.

You could add all the elements you want to above array.

about 4 years ago · Juan Pablo Isaza Report

0

Javascript should add Null as 0, but you can make sure with a function like the one at the end here. Also, as mentioned "def" and "as Double" are not valid in Javascript. This test code correctly alerts 4.5.

let val1 = returnDouble();
let val2 = retNull();
let val3 = returnDouble();
let val4 = returnDouble();

alert(val1 + val2 + val3 + val4);

function retNull() {
    return(null);
}

function returnDouble () {
    return(1.5);
}

// OR to be very sure use a Conditional Operator to make Null into a 0 integer
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

function makeNullZero(value) {
    return(Null == value ? 0 : value);

}

It seems like the components you are importing are Java (not Javascript) https://docs.atlassian.com/software/jira/docs/api/7.6.1/com/atlassian/jira/component/ComponentAccessor.html

If your variable could be assigned null, make it a Double (with a capital D) to avoid errors and convert it to zero like this:

     public static void main(String []args){
        Double val1 = makeNullZero(1.5);
        Double val2 = makeNullZero(null);
        
        Double sum = val1 + val2;
        System.out.println(sum);
     }
     
     static Double makeNullZero(Double val) {
         return null == val ? 0 : val;
     }

So for your use case:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean

// Get values from 4 custom fields
Double val1 = makeNullZero(getCustomFieldValue("FTE US"));
Double val2 = makeNullZero(getCustomFieldValue("FTE OGS IND"));
Double val3 = makeNullZero(getCustomFieldValue("FTE OGS PHL"));
Double val4 = makeNullZero(getCustomFieldValue("FTE PR"));
Double val5 = makeNullZero(getCustomFieldValue("FTE COL"));
Double val6 = makeNullZero(getCustomFieldValue("FTE Vendor"));

Double sum = val1 + val2 + val3 + val4 + val5 + val6;
return sum;

static Double makeNullZero(Double val) {
    return null == val ? 0 : val;
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!