EWM/RTC Javascript attribute customization - does return value need to be the last line in script?
I had a "Calculated Value" script to set the value of a Custom Attribute. This script had worked for a long time but suddenly started being flakey under 7.0.2 (worked sometimes and sometimes it didn't).
dojo.provide("com.example.storyPointsLastModified");dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");(function() {dojo.declare("com.example.storyPointsLastModified", null, {getValue: function(attribute, workItem, configuration) {try {// testvar lastModifiedDate = new Date();var lastModifiedDateString = dojo.date.stamp.toISOString(lastModifiedDate, {milliseconds:true, zulu:true});var txt = "The date story points were modified was: " + lastModifiedDateString;console.log(txt);return lastModifiedDateString;}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";if (typeof window !== "undefined") {window.alert(txt);}}}});})();
dojo.provide("com.example.storyPointsLastModified");dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");(function() {dojo.declare("com.example.storyPointsLastModified", null, {getValue: function(attribute, workItem, configuration) {var lastModifiedDate = new Date();var lastModifiedDateString = dojo.date.stamp.toISOString(lastModifiedDate, {milliseconds:true, zulu:true});var txt = "The date story points were modified was: " + lastModifiedDateString;console.log(txt);return lastModifiedDateString;}});})();
Accepted answer
- As far as I am aware, you can not use window alert. My understanding is, the Javascript is run in its own engine and has no connection to the web UI.
- You can use console.log. The log output goes into the eclipse workspace log.
- Calculated values and the other attribute customization provider are defining a function. You have to always return a value, otherwise the interface contract is broken and you do not get the correct behavior. In your case, if you try/catch you still have to return a valid value e.g. you can return the original value.
https://rsjazz.wordpress.com/?s=attribute+customization&submit=Search
Comments
This is not quite correct:
It has been stated by the developers in the past, that you must not use window.alert or similar capabilities.
Attribute customization can also run in the Eclipse client, where window.alert is not available and does not work.
My experience so far is, that not returning a valid value is not giving a good experience, so I make sure I know what is returned. This is especially true, because the return value is later processed in the Java SDK. How do you create an integer value from an undefined return value?
3 other answers
Comments
I had tried it with a console.log in the catch and it never entered the catch.
@Ralph Schoon:
dojo.provide("com.example.storyPointsLastModified");dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");(function() {dojo.declare("com.example.storyPointsLastModified", null, {getValue: function(attribute, workItem, configuration) {// Get the current value of the custom attribute (used in "catch" path)var storyPointsModifiedCur = workItem.getValue("storyPointsLastModified");try {var lastModifiedDate = new Date();var lastModifiedDateString = dojo.date.stamp.toISOString(lastModifiedDate, {milliseconds:true, zulu:true});var storypointsactual = parseInt(workItem.getValue("com.ibm.team.apt.attribute.complexity"), 10);var txt = "The date story points were modified was: " + lastModifiedDateString;console.log(txt);//throw new Error('Cannot update storyPointsLastModifiedAttribute');return lastModifiedDateString;}catch(err) {var txt;txt="There was an error on this page (Attribute: storyPointsLastModified was NOT updated).\n\n";txt+="Error description: " + err.message + "\n\n";txt+="Click OK to continue.\n\n";if (typeof window !== "undefined") {window.alert(txt);}return storyPointsModifiedCur;}}});})();
Comments
I couldn't fit the response in the character limit so see my answer below.
Comments
1 vote