It's all about the answers!

Ask a question

[closed] how to show the difference between current date and due date through script based calculated value?


0
1
Don Yang (7.7k21109138) | asked Jul 27 '12, 5:03 a.m.
closed Jun 30 '15, 3:24 a.m. by Ralph Schoon (63.1k33645)
I am using RTC3013 and want to show the difference between current date and due date in the task.
I followed the instruction in the below article to set the ccm/admin first.
Then create script as below

dojo.provide("ibm.example.workitems.providers.CalculatedValueSkeleton");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
dojo.require("dojo.date");
dojo.require("dojo.date.stamp");

(function() {
var WorkItemAttributes= com.ibm.team.workitem.api.common.WorkItemAttributes;

dojo.declare("ibm.example.workitems.providers.CalculatedValueSkeleton", null, {

    getValue: function(attributeId, workItem, configuration) {


    var dueDate= dojo.date.stamp.fromISOString(workItem.getValue(WorkItemAttributes.DUE_DATE));
        var currentDate= new Date();
        var dayDiff= dojo.date.difference(dueDate,currentDate, "day");
        var result = 0;
    if (dayDiff > 0) result=dayDiff;         
    }
        return result;
    }
});
})();

In Eclipse client, add new calculated value to customized attribute and point to the above js file
Create a new attribute and use the new created calculated value and set it as dependent on duedate.
Add the new attribute to task presentation.
When I create a new task, I can see the new attribute is there, but after I select a due date and save the WI, nothing happens to the attribute(blank)

I am wondering how I can achieve that: whether something is wrong in my script or I missed anything?
Would appreciate if anyone can comment on what's wrong. Thank you very much.

The question has been closed for the following reason: "The question is answered, right answer was accepted" by rschoon Jun 30 '15, 3:24 a.m.

Accepted answer


permanent link
Dinesh Kumar B (4.1k413) | answered Jul 27 '12, 10:48 a.m.
JAZZ DEVELOPER
edited Jul 27 '12, 10:49 a.m.
everything from your script looks good except for the extra "}" just after the if statement.

below is the code which worked for me
dojo.declare("org.scripts.datediff", null, {

    getValue: function(attributeId, workItem, configuration) {

           var dueDate = dojo.date.stamp.fromISOString(workItem.getValue(WorkItemAttributes.DUE_DATE));
        var currDate = new Date();
               
        var day = dojo.date.difference(currDate, dueDate, "day");
       
        return day;
    }
});
})();
Ralph Schoon selected this answer as the correct answer

Comments
Don Yang commented Jul 29 '12, 8:16 p.m.

Thanks Dinesh for pointing out the problem. I tried to remove } and it does work for me now.


Pavithra Kasturirangan commented Aug 04 '12, 6:55 a.m.

Hi, I tried the sample code above, I have made the field read-only, But even after the script automatically populates the field, it keeps the save button always enabled. Could you help to to fix this ?


Ralph Schoon commented Jul 12 '15, 6:20 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

The code has a problem and it is not working, because it does not convert the date back to something RTC can understand. You have to use fromISOString to get the date format and you have to use toISOString for the format to be converted to return it. See https://jazz.net/wiki/bin/view/Main/AttributeCustomization#API_for_Javascript

One other answer



permanent link
Ashwath G (6623550) | answered Jun 30 '15, 3:21 a.m.
Hi,

We were working on the defect age and got this code to calculate the age diff . And i wrote the code as below with reference to above code to calculate the diff of creation date and date now. And i have created an attribute of small sting and added dependency as creation date ,attached the script.But the code is not working for me.

Could you please review and guide me where it went wrong.


dojo.provide("example.calculated.defectAgeValueProviderfix3");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
dojo.require("dojo.date.stamp");

(function() {
var WorkItemAttributes = com.ibm.team.workitem.api.common.WorkItemAttributes;
var fromISOString = dojo.date.stamp.fromISOString;
  dojo.declare("example.calculated.defectAgeValueProviderfix3", null, {

    getValue: function(attribute, workItem, configuration) {

      var creationDate = dojo.date.stamp.fromISOString(workItem.getValue(WorkItemAttributes.CREATION_DATE));

console.log("status enter");
console.log(creationDate);

var currentDate = new Date();
    var dayDiff= dojo.date.difference(creationDate,currentDate, "day");
        var result = 0;
    if (dayDiff > 0) result=dayDiff;        
   
        return result;
    }
  });
})();