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).
I started playing with it in our DEVELOPMENT environment (adding some console.log statements) and discovered that it wouldn't set the custom attribute at all. I could tell from the browser's "Development Tools"/"Console" and my "console.log" that the script was being invoked/executed but the value would never be updated.
I decided to take out the try/catch (originally had a try/catch block). When I took out the try catch such that the last line in the script was the return value, the script worked/updated the custom value.
Are try/catch blocks forbidden in calculated values?
Script before (wouldn't update value):
Script after (successfully updates value):
|
Accepted answer
![]()
Ralph Schoon (63.3k●3●36●46)
| answered Dec 07 '23, 2:10 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
My experience is shared here:
https://rsjazz.wordpress.com/?s=attribute+customization&submit=Search Becky McDermott selected this answer as the correct answer
Comments This is not quite correct:
- window.alert will work in the web UI but will throw an exception in the Eclipse UI and on the server. Since Becky's code checks for a valid window object before using it, this is all legal and will not cause problems.
- console.log will work in all three places. In the web UI it writes to the console in the developer tools for the browser, on the server it writes into the log file
- in Javascript, by default, if a function's execution doesn't end at a return statement, or if the return keyword doesn't have an expression after it, then the return value is 'undefined'. The return statement simply allows you to return an arbitrary value from the function. 'undefined' would be parsed as a null or zero in the attribute code
It has been stated by the developers in the past, that you must not use window.alert or similar capabilities.
Hi Ralph,
You can use Web UI only capabilities as long as they are properly protected - Becky's code example is doing just that. If you reread my response above this will be clearer - there are three places where the code runs: the Web UI, the Eclipse UI and the server. Each of these places has elements not present in the others, but you can write code that uses them if properly protected as above.
You do not create an integer from an undefined value, you must test the return and respond appropriately. It appears the internal code is doing that -it would need to do that even if you explicitly returned a value, as even something like parseInt() will return NaN for any input that can't be converted into an integer
|
3 other answers
![]() @Ralph Schoon:
Thank you for your explanation. It was very helpful. I was finally able to get it working as desired by adding the return of the original value in the catch as you suggested. If I uncomment the "throw new Error" line and step through the debugger (setting ?debug=true and using the browser's developer tools to load the script), I can see that the "window.alert" does display a diaglog box which is good.
The final script looks like:
Thank you for your response.
Comments
See my comment against Ralph's reply. If adding a formal return statement to your catch block gives you a value then that means your catch block is being triggered, possibly by the window.alert function trying to run on the server but possibly due to something else.
Leaving out a return is perfectly legal Javascript, and will mean the function returns the Javascript default value 'undefined', which will then be parsed by the attribute's internal code as a null or zero and so you'll get no value displayed.
I couldn't fit the response in the character limit so see my answer below. |
Your answer
Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.