Forums

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

How can I get a calculated custom field to use integer values instead of character decimal values?

Pam Weber June 26, 2017

I've been trying to get a calculated text field to work (using the Innovalog Misc Custom Fields Add-On) without luck. I'm trying to evaluate the values in two custom fields and then return a value in the calculated text field.

From this page - https://innovalog.atlassian.net/wiki/display/KB/Using+issue.get%28<field_name>%29+in+scripts – I determined that my custom fields, both of type 'Select List (single choice)', return string values.

The first character in both fields is a number. I need to add the first character from each field and then based on the sum, return a string of "High" (>8), "Medium" (>4), or "Low".

I have tried so many different approaches and I continue to see the result of my addition to be the sum of the decimal values of the numbers, not the sum of the integers. For example, if both fields start with "1", the result of the addition is 98 (NOT 2) - because the decimal value of character "1" is 49. Likewise, if one of the fields starts with "1" and the other starts with "2", then the sum is 99 (NOT 3).

This is the most recent code that I have tried:

Calculated value to illustrate relative risk, based on Risk Likelihood and Impact values.
<!-- @@Formula:

if (issue.get("Likelihood")==null || issue.get("Risk Impact")==null || issue.get("Likelihood")== "None" || issue.get("Risk Impact")== "None")
    return "n/a";
else {
    int iLikelihood;
    iLikelihood = Integer.valueOf(issue.get("Likelihood").charAt(0));

    int iProjectRisk;
    iProjectRisk = Integer.valueOf(issue.get("Risk Impact").charAt(0));

    int iSum;
    iSum = Integer.valueOf(iLikelihood) + iProjectRisk;

    if (iSum > 7)
        return "High";
    else if (iSum > 4)
        return "Medium";
    else
        return "Low";
    }
-->

NOTES:

1) I tried to use Integer.parseInt() to save the first character of each field as an integer :
    int iLikelihood;
    iLikelihood = Integer.parseInt(issue.get("Likelihood").charAt(0));
but got the following error :
    "Error in method invocation: Static method parseInt( char ) not found in class'java.lang.Integer'".
so I switched to using Integer.valueOf()
    int iLikelihood;
    iLikelihood = Integer.valueOf(issue.get("Likelihood").charAt(0));

2) I tried to store the first character of each field in a string variable (since the source field is of type String) :
    String sLikelihood;
    sLikelihood = issue.get("Likelihood").charAt(0);
but got the following error :
    "Variable assignment: sLikelihood: Can't assign primitive value to object type:class java.lang.String"
so switched to storing the first character in an integer variable
    int iLikelihood;
    iLikelihood = issue.get("Likelihood").charAt(0);

I tried many different combinations of using the Integer.valueOf() method to get the variables into integer format, but they always resulted in the sum being 98 or higher.

I've been working on this for over 2 days and just can't find any permutation that works! Any help that can allow me to get actual integer addition to work would be greatly appreciated!! (In the mean time, I changed my if-else rules to check for > 100 and > 103 (instead of > 4 and > 7), which is giving me the final result that I'm looking for - but isn't really the cleanest way to do this.)

Thank you!

1 answer

0 votes
Myra_Rebillard
Contributor
September 11, 2018

Hi Pam,

parseInt reads a string, not a character.

Try the following - I separated the two so you can print out what you get using the logging output.

int iLikelihood  Integer.parseInt(issue.get("Likelihood"));

Hope that helps!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events