Help with a custom work item attribute “Calculated Value”
Hi,
Testing out a potential use of a calculated value attribute in RTC. I’m not much of a coder and I think I’m having issues with attribute types/Variable types.
I have two attributes: rate (decimal) and qty (integer). I want to multiply the two in a scripted calculated value provider and save it as an attribute cost (decimal).
Using workItem.getValue(attributeID) I’m struggling to extract the numbers to use in the simple calculation. Can anyone help?
Thanks
Glyn
Accepted answer
Support case was opened. Case is closed, there was an issue with the process configuration, as already hinted here.
This caused the null value for the attribute.
3 other answers
Can you post your code up?
The core calculation should look something like this:
var result = parseFloat(workitem.getValue(rate)) * parseFloat(workitem.getValue(qty));
return Number.isInteger(result) ? result + ".0" : result.toString();
Since you specified that the result should be a decimal, the second line will check the result and add a decimal point into a result that's an integer
Comments
In addition to Davyds answer, please
1. Have a look at https://www.w3schools.com/js/default.asp The site has helped me greatly with tips.
2. Learn how to use the Chrome Debugger
The debugger can be used this way:
Open the work item menu for your project area in Chrome.
Inject ?debug=true into the URL in front of the #
e.g. https://elm.example.com:9443/ccm/web/projects/JKE Banking (Change Management)?debug=true#action=com.ibm.team.workitem.viewWelcome and run the tab again.
Use CTRL+SHIFT+I to open the developer tools.
Use the three vertical dots to the right of the cogwheel to change the location of the developer tools to the bottom.
Select the Sources tab.
Use a query or the Search Work Items to locate a work item or create one.
You will see two cloud icons, one shows your server and port e.g. elm.example.com:9443 and another that shows "(no domain)"; open the latter one. Find your calculated value script and open it. Set a breakpoint in the beginning.
When the script is run, the page will stop and you can step through the script. You can hover over variables to understand their form, types and values.
If you change the script and save it in Eclipse, make sure to completely reload the page in the browser to get the newest script version.
If you change the script and save it in Eclipse, make sure to completely reload the page in the browser to get the newest script version.
You can now debug your script (note does not work for default value scripts).
In the debugger, it seems to get the decimal attribute rate_1 ok, but is struggling to get the "Integer" attribute qty_1
dojo.provide("calculated.budgetTotal");
(function() {
dojo.declare("calculated.budgetTotal", null, {
getValue: function(attribute, workItem, configuration) {
var r1 = workItem.getValue('rate_1');
var q1 = workItem.getValue('qty_1');
var tot1 = parseFloat(r1) * parseFloat(q1);
return tot1.toString();
}
});
})();
(function() {
dojo.declare("calculated.budgetTotal", null, {
getValue: function(attribute, workItem, configuration) {
var r1 = workItem.getValue('rate_1');
var q1 = workItem.getValue('qty_1');
var tot1 = parseFloat(r1) * parseFloat(q1);
return tot1.toString();
}
});
})();
the debugger returns:
r1: (number)
q1: null (workItem.getValue: undefined)
I've double checked the attributeIDs, all seems to tie up correctly.
I tried changing q1 to a different (decimal) attribute and it still returns null. I can't seem to get a second attribute for the calculation.