Set the Due Date automatically based on creation date
I need to set the Due Date attribute automatically as X days ahead of creation date for Defect WI.
for e.g. Due Date = Creation Date + 7 days
I wrote a dojo script for that in Attribute Customization -> Calculated Values. And then edited the Due Date attribute for Defect WI to get the Calculated Value from the script.
But may be somewhere my script is wrong or something else, so it's not working.
Please check the script I wrote:
dojo.provide("com.example.DueDateProvider");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
(function() {
dojo.declare("com.example.DueDateProvider", null, {
getValue: function(attribute, workItem, configuration) {
if(workItem.getValue(WorkItemAttributes.TYPE) == "defect" ){
// How to set the value of Due Date atrribute? I guess below line is not correct.
WorkItemAttributes.DUE_DATE = workItem.getValue(WorkItemAttributes.CREATION_DATE) ;
}
return workItem.getValue(WorkItemAttributes.DUE_DATE);
}
});
})();
Accepted answer
Check https://jazz.net/library/article/1093 and check the last lab. Do some debug outputs to the console and read them.
check https://jazz.net/library/article/1360 and try debugging. Use Crome for that.
At the moment I would not even know if you enabled scripting or anything works as you do not provide any useful information. The code below worked for me - does not help if we don't even know if you are able to use it.
You did not seem to have read https://jazz.net/wiki/bin/view/Main/AttributeCustomization the section about conversion of dates very good, either. See the conversion direction below and compare it to yours.
/*********** * 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.team.example.CurrentDate");dojo.require("dojo.date"); // We need the date class from Dojo dojo.require("dojo.date.stamp"); // We need the stamp class to work with ISO date strings
(function() { dojo.declare("com.team.example.CurrentDate", null, {
getDefaultValue: function(attribute, workItem, configuration) { var now=new Date(); var date= dojo.date.stamp.toISOString(now, {milliseconds:true, zulu:true}); console.log("Now is: " + date); return date; } });
})();
Comments
I got to know the problem was in work item type condition,
if(workItem.getValue(WorkItemAttributes.TYPE) == "defect" )
When I commented above line, simple return statement was reflecting in due date field:
return workItem.getValue(WorkItemAttributes.CREATION_DATE);
Is there any issue in conditional statement?
Now I updated the script to add 7 days without WI type condition. It works but while I create the WI, it shows default value in due date field as "Jan 8, 1970, 5:30:00 AM" which gets updated once I save the WI. Is there a way to show nothing when we just click to create WI?
(function() {
var WorkItemAttributes= com.ibm.team.workitem.api.common.WorkItemAttributes;
dojo.declare("com.example.DueDateProvider", null, {
getValue: function(attribute, workItem, configuration) {
var creationDate = dojo.date.stamp.fromISOString(workItem.getValue(WorkItemAttributes.CREATION_DATE));
var dueDate = dojo.date.add(creationDate, "day", 7);
var date= dojo.date.stamp.toISOString(dueDate, {milliseconds:true, zulu:true});
return date;
}
});
})();
In addition to default due date value, there is another issue that we can't change the due date to any other date. When I change it, while saving it reverts back to the calculated date (creation date + 7 days)
- The creation date is NOW
- You can hide an attribute presentation on creation
In addition to default due date value, there is another issue that we can't change the due date to any other date. When I change it, while saving it reverts back to the calculated date (creation date + 7 days).
The conditional statement is fixed.
@Ralph
Sorry, I didn't get this:
-
The creation date is NOW
- You can hide an attribute presentation on creation
May be I should ask a separate question for the issues.
2 other answers
JavaScript provides ways like dojo.date to work with dates.
You can not simply add some stuff and hope to get something useful what would Apple+5 be?
The following code shows some ways how working with dates can be done. Note that you have to convert back and forth as described in https://jazz.net/wiki/bin/view/Main/AttributeCustomization
// Work Item new: not initialized - provides todays date. Once created provides the creation date var beginDate = dojo.date.stamp.fromISOString(workItem.getValue("com.ibm.team.workitem.attribute.creationdate")); // Get the current attribute's value and make a Date object from it console.log("I got: ["+workItem.getValue(attributeId)+"]"); var endDate= dojo.date.stamp.fromISOString(workItem.getValue(attributeId));
// Compare the two dates returns negative value due to order var compare = -(dojo.date.difference(endDate, beginDate, "day")); if (compare>= 30) { // make sure endDate is not earlier than beginDate + 30
I'm surprised to see so many mistakes in such simple script. Put it bluntly, of those lines that you need to modify and have modified from the example skeleton, none is correct (not just the calculation of date). I strongly suggest you read the API specification first before trying to write any codes.
https://jazz.net/wiki/bin/view/Main/AttributeCustomization
Note that the script does not appear to be triggered by the Creation Date attribute at all. It may be due to the way (timing) the system sets its value. You may need to use other mandatory attribute such as Summary to trigger the script.
Comments
Yes, you're right. I modified it from the example skeleton. I'm new bee to this, didn't have any idea where to refer to API's for it. Thank you for your suggestion.
I updated the script, but not getting how to set the Due Date attribute to some value. As of now I'm trying to set it to Creation Date value, later I'll modify to add 7 days.
You can not set an attribute value in an attribute customization script. You can create a calculated value and return the value.
Perform https://jazz.net/library/article/1093 the RTC Only version or at least work through the last lab.
Also read the help topic and the related links:
https://jazz.net/help-dev/clm/topic/com.ibm.team.workitem.doc/topics/c_calc_val_attribute_config.html
I changed the script to first set the Due Date attribute as of creation date only just to see if it reflects, but it's not happening. Please check the code;
dojo.provide("com.example.DueDateProvider");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
dojo.require("dojo.date");
dojo.require("dojo.date.stamp");
(function() {
dojo.declare("com.example.DueDateProvider", null, {
getValue: function(attribute, workItem, configuration) {
if(workItem.getValue(WorkItemAttributes.TYPE) == "defect" ){
return dojo.date.stamp.fromISOString(workItem.getValue(com.ibm.team.workitem.attribute.creationdate));
}
}
});
})();
I tried to return in this way also, but not working.
// return dojo.date.stamp.fromISOString(workItem.getValue(WorkItemAttributes.CREATION_DATE));
// return workItem.getValue(WorkItemAttributes.CREATION_DATE);
I tried setting the dependencies to many attributes but nothing worked.