custom attribute script concatenates instead of adding
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();
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
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.
Comments
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
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.
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.
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.