It's all about the answers!

Ask a question

why my calculated value script run even when the change is not triggered?


jane zhou (106865) | asked Jun 27 '17, 4:46 p.m.
edited Jun 27 '17, 5:40 p.m.
Hi, Someone who may concern,

 

We found a very strange issue lately. 
We have 2 projects, running the same calculate value script. The purpose is that when status is changed to close, then the date will be recorded
I have configured the Dependencies as "Status" for both projects, i.e. only when user change status to close, this script will run.

 dojo.provide("client.valueProvider.currentCloseDate"); 
 dojo.require("dojo.date.stamp"); 
 dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
 
 (function() { 
     var WorkItemAttributes = com.ibm.team.workitem.api.common.WorkItemAttributes; 
     dojo.declare("client.valueProvider.currentCloseDate", null, { 
         getValue: function(attribute, workItem, configuration) { 
             var now = dojo.date.stamp.toISOString(new Date(), {milliseconds:true, zulu:true});
             var state = workItem.getValue(WorkItemAttributes.STATE); 
             var myOldValue = workItem.getValue(attribute); 
              if (state ===='xxx.state.close') {            
                        return now;                
                 } 
             else
               return myOldValue; 
         } 
     }); 
 }) ();   

However, on the old project, it can work properly.
But the new project, which is created with the same script,  it cannot work properly:
as long as I refresh the page for a work item with status as "Close", the close date will be updated to current time.  It seems my script run without trigger of status change.
However, I also noticed for the same project, defect can not work, but task can work. both of them share the same attribute for the close date.

Then I added break point in my script, and refresh the page, the break point can not be reached.
And I tried to add a console.log in the script to make sure whether the script run, but console did not show anything.
It seems this script run before tracing ability is enabled.

Because as long as I remove the script, the correct close date can be shown properly. So I am 100% sure there are something wrong for running this script.

I am wondering any configuration may cause such issue?

Thanks!

Best Regards,
Jane Zhou






3 answers



permanent link
Ralph Schoon (63.1k33646) | answered Jun 27 '17, 6:08 p.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

I think the problem here is your assumption that you can detect a state change in a JavaScript attribute customization (calculated value?) for all I know you can't. The only ways to be able to do so would be the ability to access the old state which you don't have, access to the action which you don't have except in conditions or storing the old state in a hidden attribute, which I don't see.


I have summarized my experience and common misconceptions here: https://rsjazz.wordpress.com/2016/07/15/rtc-process-customization-what-you-can-and-cannot-do/



Comments
jane zhou commented Jun 27 '17, 9:47 p.m.

 Hi Ralph,

        Thanks for your reply!
        Please see my questions as follows. Thanks!

Best Regards,
Jane Zhou


permanent link
jane zhou (106865) | answered Jun 27 '17, 9:44 p.m.
edited Jun 27 '17, 9:46 p.m.

 Hi Ralph,

       Thanks for your reply!
       The part you did not see is configured in Eclipse client to add Dependencies to the attribute close_date with calculated value. The purpose is  that only when  dependent attribute change then related calculated value script will run.
        For my case, the calculated value is attached to attribute close_date, and dependent attribute is Status.
        The same attribute close_date is shared between type of Defect and Task. However, it can work for task, but can not work for defect. 
         So, if my script can not work, they should not work for both cases. why it can work for task, but not for defect?
         And in fact, I noticed the history did not show the close_date is updated  to current time when I refresh the work item page. i.e. it only displays that way, but in database, it still record the original close_date which triggered by state change. 
         Thanks!

Best Regards,
Jane Zhou


permanent link
Donald Nong (14.5k414) | answered Jun 27 '17, 10:57 p.m.

You just made things very confusing.

Refreshing a page does not change anything of the work item. If you see that an attribute has an incorrect value, it is a "display" issue.

The calculated value provider will be executed at different timings, and you should understand when it will be executed.
https://jazz.net/wiki/bin/view/Main/AttributeCustomization#Script_based_calculated_values

As far as I know, reusing an attribute does not share the provider configurations.

And finally, different types of work items are likely using different workflows, hence the state ID can be different. Make sure that you specify the correct state ID in your code.


Comments
jane zhou commented Jun 28 '17, 10:43 a.m. | edited Jun 28 '17, 9:24 p.m.

 

 Hi Donald,
          We have the same workflow for defect and task.
           And I have compared the process configuration source xml file for the previous project and current project. They are totally the same for all lines related to this attribute except the link to java script because they are in different project.
           I know when calculated value will be executed, because I have used it lot in our project. 
           Please see the part related to close date in xml file as follows:

<customAttributes category="com.ibm.team.workitem.workItemType">
 ...
 <customAttribute id="com.xxx.closedate" name="Close Date" type="timestamp"/>
 ...
</customAttributes>
<customAttributes category="com.ibm.team.workitem.workItemType.task">
    ...
    <customAttribute id="com.xxx.closedate" name="Close Date" type="timestamp"/>
    ...
</customAttributes>
...
<attributeDefinition id="com.xxx.closedate" name="Close Date" type="timestamp">
<dependsOn id="com.ibm.team.workitem.attribute.state"/>
<valueProvider providerId="com.ibm.team.workitem.valueproviders.VALUE_PROVIDER._QwfAAJ9FEeaD-oSyteFouw"/>
</attributeDefinition>      
<section id="com.xxx.closedate">
 <presentation attributeId="com.xxx.closedate" kind="com.ibm.team.workitem.kind.timestamp">
        <property key="hideIfEmpty" value="true"/>
        <property key="hideIfCreation" value="true"/>
        <property key="readonly" value="true"/>
 </presentation>
</section>

<tab id="com.ibm.team.workitem.tab.task.overview" layout="builtInOverviewLayout">
 ...
 <section sectionId="com.xxx.closedate" slot="quickInfo" title="Close Date"/>
 ...
 </tab>
<tab id="com.ibm.team.workitem.tab.overview" layout="builtInOverviewLayout">
 ...
<section sectionId="com.xxx.closedate" slot="quickInfo" title="Close Date"/>    
 ...
</tab>

Best Regards,
Jane Zhou


jane zhou commented Jun 28 '17, 10:47 a.m.

 Hi Donald,


         Please see my comments as follows.
         Thanks!

Best Regards,
Jane Zhou


jane zhou commented Jun 28 '17, 1:39 p.m. | edited Jun 28 '17, 9:24 p.m.

 

Hi Donald,

      When I refresh the page for an existing work item , it did not meet all 3 conditions for the calculated value.  But why it is executed? This is very confusing.

Script-based calculated values

Note: You can find general information about script-based attribute customization in the Using scripts for attribute customization section.

If you configure a Script-based calculated value, the script will be executed in three 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.
Best Regards,
Jane Zhou


Donald Nong commented Jun 29 '17, 4:59 a.m.

I'm still not sure what's going on, but here are the things you can consider.
1. When you refresh the page, the values come from the server, so you should look at the server log if you do put the console.log() for debugging purpose.
2. You cannot detect a "change" of the state using JavaScript, as you cannot retrieve the "old" value, which Ralph has already pointed out. While you can detect the "action" being used, it only works in a Condition provider, not Calculated Value provider.
3. The "Close Date" presentation appears to be read-only, is it the reason it does not get updated?

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.