RTC Calculated Value triggering for any change in status or attribute
I have a script which should update a counter based on changes in owner field. I put the counterstring having dependency on Owned By. Following is the script:
dojo.provide("counterString");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
(function() {
dojo.declare("counterString", null, {
getValue: function(attribute, workItem, configuration) {
try { var counter = parseFloat(workItem.getValue("counterstring"));
console.log("Count String is 1: " + counter + "\n\n");
if (isNaN(counter)=== true)
{
console.log("Count String now is 2: " + counter + "\n\n");
counter = 0 ;
console.log("Count String now is: 3" + counter + "\n\n");
}
else
{
console.log("Count String now is 4: " + counter + "\n\n");
counter = counter + 1;
console.log("Count String now is 5: " + counter + "\n\n");
console.log("Exiting Counter String");
}
return counter.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("Inside ERR Counter String" + txt + "\n\n");
}
}
});
})();
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
(function() {
dojo.declare("counterString", null, {
getValue: function(attribute, workItem, configuration) {
try { var counter = parseFloat(workItem.getValue("counterstring"));
console.log("Count String is 1: " + counter + "\n\n");
if (isNaN(counter)=== true)
{
console.log("Count String now is 2: " + counter + "\n\n");
counter = 0 ;
console.log("Count String now is: 3" + counter + "\n\n");
}
else
{
console.log("Count String now is 4: " + counter + "\n\n");
counter = counter + 1;
console.log("Count String now is 5: " + counter + "\n\n");
console.log("Exiting Counter String");
}
return counter.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("Inside ERR Counter String" + txt + "\n\n");
}
}
});
})();
This script executes every time the State or any attribute is changed. I only want script to execute when the Owned By is changed. As of now when Owned By is changed, the script executes twice, one for Owned By Change and second for the save.
2 answers
Per document https://jazz.net/wiki/bin/view/Main/AttributeCustomization#Script_based_calculated_values
the script will be executed in 3 cases:
- When a work item is created, the script is executed
- When a work item is saved, the script is executed to recalculate the value.
- When an attribute which the current attribute depends on is changed, the value of the current attribute is recalculated. Depending on the presentations of the two attributes this may not work in all cases.
This is very hard to do. An approach you could try is:
Another approach would be to not use JavaScript but a Java based follow up action. In a follow up action you have the old state of the work item and the new state and you can compare them and then issue a second save to increase the counter. See https://rsjazz.wordpress.com/2012/07/31/rtc-update-parent-duration-estimation-and-effort-participant/ for an example. This extension would have to be deployed on the server and configured as operational behavior.
-
Use a read only, or hidden attribute that is a calculated value and stores the user ID.
- Your current counter script would be dependent on this attribute and only trigger when that changes.
Another approach would be to not use JavaScript but a Java based follow up action. In a follow up action you have the old state of the work item and the new state and you can compare them and then issue a second save to increase the counter. See https://rsjazz.wordpress.com/2012/07/31/rtc-update-parent-duration-estimation-and-effort-participant/ for an example. This extension would have to be deployed on the server and configured as operational behavior.