It's all about the answers!

Ask a question

custom attribute script concatenates instead of adding


Kiran Poovaiya (611) | asked Sep 24 '13, 1:46 a.m.
My script works fine with eclipse client but with webclient the numbers are getting concatenated instead of adding.
This script was working fine until I added on extra addition toward the end of the script.

ojo.provide("CalculateHLDSizing");

var f = function() {
    dojo.declare("CalculateHLDSizing", null, {
          getValue: function(attributeId, workItem, configuration) {
                 var result = workItem.getValue("hldArchitectureHours") +
                workItem.getValue("hldDeploymentHours") +
                workItem.getValue("hldmacroDesignHours") +
                workItem.getValue("hldAPIhours")+
                workItem.getValue("hldGUIHours")+
                workItem.getValue("hldInputETLHours")+
                workItem.getValue("hldCoreDBHours")+
                workItem.getValue("hldReportsHours")+
                workItem.getValue("hldOutputETLHours")+
                workItem.getValue("hldUATHours")+
                workItem.getValue("hldSystemTestHours") +
                workItem.getValue("hldDocumentationHours") +
                workItem.getValue("hldTrainingHours") +
                workItem.getValue("hldASCAHours");
            return result;
        }
    });
};

f();

Accepted answer


permanent link
Ralph Schoon (63.1k33646) | answered Sep 24 '13, 3:48 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
JavaScript is kind of weird (from my perspective). When creating the Process Enactment Workshop for the Rational solution for Collaborative Lifecycle Management 2012, I realized that it might treat things as string where I wanted integer. In Lab 5.2 I used parseInt() to convert strings to integer and then add the data.
Kiran Poovaiya selected this answer as the correct answer

Comments
Kiran Poovaiya commented Sep 24 '13, 7:15 a.m.

Thankss , this helped , I added parseInt around the values and it worked like a charm.
                var result = parseInt(workItem.getValue("architecture_hr_rom")) +
                  parseInt(workItem.getValue("macroDesignROMHours")) +
                  parseInt(workItem.getValue("romDeploymentHours"))+
                      .....          parseInt(workItem.getValue("romASCAHours"));
            return result;
   

One other answer



permanent link
Dinesh Kumar B (4.1k413) | answered Sep 24 '13, 4:56 a.m.
JAZZ DEVELOPER
edited Sep 24 '13, 4:59 a.m.
So the underlying thing seems to be that one of your attributes is of type string.

Its most likely the ones which you added latest and soon after started noticing the concatenation.
So, I would suggest reviewing the the types of the attributes.

Here is something more to make it easier to follow:

Below is a script that runs on
-  two Integer attributes (firsthours_int, secondhour_int) and
-  a string attribute (thirdhour_str). 

As long as I use the values read from Integer attributes, the return is an Integer
As soon as I start using the "thirdhour_str", the return is a concatenated one.

dojo.provide("com.example.sumitup");

(function() {
    dojo.declare("com.example.sumitup", null, {

        getValue: function(attribute, workItem, configuration) {

var fh = workItem.getValue("firsthours_int");
var sh = workItem.getValue("secondhour_int");
var th = workItem.getValue("thirdhour_str");

var sum = fh + sh + th;  //returns a concatenated of (sum of fh and sh) and th
var sum = fh + sh;  // returns an integer

return sum;

        }
    });
})();

Like Ralph already pointed out, your solution is in using parseInt() when working with strings. 
However, I believe instead of Integer attributes, you have gotten a string typed attribute which is causing the failure for you.

Hope this helps.

Comments
Ralph Schoon commented Sep 24 '13, 5:05 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Great answer Dinesh, I was deliberately using String type attributes in my example to be able to specify 1w for one week.


Dinesh Kumar B commented Sep 24 '13, 5:45 a.m.
JAZZ DEVELOPER

thanks Ralph :)

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.