[closed] how to show the difference between current date and due date through script based calculated value?
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
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;
}
});
})();
Comments
Thanks Dinesh for pointing out the problem. I tried to remove } and it does work for me now.
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 ?
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
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;
}
});
})();