It's all about the answers!

Ask a question

Calculated value execution cases


Angelo Corna (26358383) | asked Jun 21, 5:44 p.m.
From my understanding, a calculate value script will be executed in 3 cases:
  • When a work item is created
  • When a work item is saved
  • When an attribute which the current attribute depends on is changed
Is there te possibility to check which case is executing the script?

I would like to have different behavior depending on the various cases.

Thanks in advance.
Bye.

Accepted answer


permanent link
Davyd Norris (2.6k217) | answered Jun 21, 9:16 p.m.
edited Jun 23, 8:59 a.m.
I don't believe the third case happens unless the middle case is true.
I don't think the calculation happens in real time as the value is changed from what I remember, but then I'm not sure you would want a different case for the real time vs the save - what the value ends up being on the screen should be what is saved.

You can catch the creation case pretty easily by checking the State attribute - if the work item has just been created then workItem.getValue(WorkItemAttributes.STATE) === null

EDIT: I just checked and you are correct - the calculation is done in the client in real time. This means you can differentiate between the second case and the third by checking where the calculation was triggered. The real time case is done in the client, and the Save case is done on the server, so you can check for the window Object. If it's defined then you're in the client, if not then you're on the server
Angelo Corna selected this answer as the correct answer

2 other answers



permanent link
Angelo Corna (26358383) | answered Jun 23, 9:50 a.m.
Hi Davyd, thanks for your response.

Consider this use case (it's not my really use case but it's ok for an example)

We have 3 fields
  • FieldA (Small String, ReadOnly)
  • FieldB (Small String)
  • Action (Enumeration)
When Action field value is Modify I want that the value of FieldA is copied on FieldB.
After this I can change the value of FieldB and I can save the WorkItem. After the save FieldA must contains the original value and FieldB must contains the new one.

In order to copy the value of FieldA to FieldB, I've create a simple calculated value on FieldB with Action dependency  and it works without problems, when Action field is set to Modify the value of FieldA is copied on FieldB and I can change the FieldB value.

Unfortunately, when I save the WorkItem, the calculated vale runs again and overrides the new value inserted on FieldB.

So, if I'm able to understand how the calculated value script is fired (for Action field change or for save Workitem) I'm able to correctly manage this use case on JavaScript.

Thanks. Bye.




Comments
Davyd Norris commented Jun 23, 6:44 p.m.

So, in that case, check for the existence of the window Object - that only exists when you're in the client. If it exists then do the copy, if not then don't.


Angelo Corna commented Jun 24, 3:14 a.m. | edited Jun 24, 6:42 a.m.

How can I check it?


Davyd Norris commented Jun 24, 8:26 p.m.

/***********
Licensed Materials - Property of IBM
(c) Copyright IBM Corporation 2011. All Rights Reserved.

Note to U.S. Government Users Restricted Rights: 
Use, duplication or disclosure restricted by GSA ADP Schedule
Contract with IBM Corp.
***********
/
dojo.provide("com.example.ValueProvider.checkLocation");

(function() {
    dojo.declare("com.example.ValueProvider.checkLocation", null, {

        getValue: function(attribute, workItem, configuration) {
  return window ? "client" : "server";
        }
    });
})();


Davyd Norris commented Jun 24, 9:32 p.m.

    getValue: function(attribute, workItem, configuration) {
      console.log('here');
      if (workItem.getValue(WorkItemAttributes.STATE) === null) {
        return 'created';
      }
      if (window) {
        return 'modified';
      }
      return 'saved';

    }

I just checked this and the server side save never fires - I can edit the calculated value and it just saves it


permanent link
Ralph Schoon (63.4k33646) | answered Jun 24, 6:47 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

The API is explained here: https://jazz.net/wiki/bin/view/Main/AttributeCustomization#Calculated_values 


I am not aware of an official mechanism that would reveal why the calculated value is run.

I am aware, that users have looked into the API, especially in the browser. There are more functions and objects available in the browser. Some users have used them, even if they are not documented. This can work, but might break in the future. Please be aware, that those objects and methods might not be accessible in the Eclipse client environment.

Your answer


Register or to post 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.