Calculated Value not working
I have a script to calculated value bases on 2 other attributes. When I change either of the 2 attributes, nothing happens. any ideas, here is the script: we are on 6.0.1 ifix 003
dojo.provide("com.ce.team.workitem.attribute.riskProbabiltyCostProvider");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
(function() {
dojo.declare("com.ce.team.workitem.attribute.riskProbabiltyCostProvider", null, {
getValue: function(attribute, workItem, configuration) {
try {
var consequenceCost = parseInt(workItem.getValue("com.ibm.team.workitem.workItemType.risk.consequencecost"));
var probabilityAttribute = configuration.getChild("probabilityAttribute").getIdentifier()
var probability = this.__getLastIntSegment(workItem.getValue(probabilityAttribute));
var calcCost = (consequenceCost * probability) / 100
return calcCost.toString();
}
catch(err) {
var txt;
txt="There was an error on this page.\n\n";
txt+="Error description: " + err.message + "\n\n";
txt+="Click OK to continue.\n\n";
alert(txt);
}
}, //end getvalue
__getLastIntSegment: function(identifier) {
if (identifier != null) {
var lastSeparator = identifier.lastIndexOf('.');
var numberString = identifier.substring(lastSeparator+2);
return parseInt(numberString, 10);
}
return -1;
},
__sentinel: null
});
})();
Accepted answer
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
(function() {
dojo.declare("com.ce.team.workitem.attribute.riskProbabilityCostProvider", null, {
getValue: function(attribute, workItem, configuration) {
try {
//console.log("Executing cost provider");
var consequenceCost = parseInt(workItem.getValue("com.ibm.team.workitem.workItemType.risk.consequencecost"));
//console.log("Cost = " + consequenceCost);
var probabilityAttribute = workItem.getLabel("com.ibm.team.workitem.workItemType.risk.probability");
//console.log("Prob Enum: " + probabilityAttribute);
var probability = this.__getLastIntSegment(probabilityAttribute);
//console.log("Probability: " + probability);
var calcCost = (consequenceCost * probability) / 100;
return calcCost.toString();
}
catch(err) {
var txt;
txt="There was an error on this page.\n\n";
txt+="Error description: " + err.message + "\n\n";
txt+="Click OK to continue.\n\n";
console.log(txt);
}
}, //end getvalue
__getLastIntSegment: function(identifier) {
if (identifier != null) {
var lastSeparator = identifier.lastIndexOf('%');
//console.log("Sep index: " + lastSeparator);
var numberString = identifier.substring(0,lastSeparator);
//console.log("Number: " + numberString);
return parseInt(numberString, 10);
}
return -1;
},
__sentinel: null
});
})();
2 other answers
- Go to the administrative page of your RTC server https://your.server.name:9443/ccm/admin
- Open the Server tab
- From the left side-bar open Configuration > Advanced Properties
- In the Work Item Component find the Enable Process Attachment Scripts property and set its value to true
- Save your changes
https://jazz.net/forum/questions/219896/i-want-to-use-a-calculated-field-to-sum-an-attribute-from-all-children
Secondly, if you make any mistakes in the script, the function __getLastIntSegment() will always return -1. Whether it's visible to you or not, I'm not sure.
Thirdly, make sure you have configure the target attribute to be dependent on the other two attributes, which brings up a messy issue - why can't you use the Id of "probabilityAttribute" in the script? It should be static anyway, otherwise you cannot set it in the Depends On field of the target attribute.
And finally, use console.log() to debug your code. Don't use alert(), as it may only work in a browser, but the script can run in a browser, Eclipse and on the server. And check the server log file and browser web console - this funny looking script may render all the JavaScript script useless (RTC seems to evaluate all scripts as a whole).