How to compare two Date attributes in a script-based validator?
Hi Ralph,
I need to write a dojo script where I can compare 2 dates, one is IBM out of the box field due date and other is system current date. I am writing a validator script where users will not be allowed to enter the Due date < Current Date. Due Date can be equal to or greater than current date. Below is the dojo script for the same.
dojo.provide("DueDateValidation");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes"); // To access the work item attributes
dojo.require("com.ibm.team.workitem.api.common.Status"); // To create a status object to return
dojo.require("com.ibm.team.workitem.api.common.Severity"); // To create a severity for the status
dojo.require("dojo.date"); // We need the date class from Dojo to compare two dates
dojo.require("dojo.date.stamp"); // We need the stamp class to work with ISO date strings
(function() {
var Status = com.ibm.team.workitem.api.common.Status;
var WorkItemAttributes = com.ibm.team.workitem.api.common.WorkItemAttributes;
var DateValidator = dojo.declare("DueDateValidation", null, {
validate: function(attributeId, workItem, configuration) {
var severity = "ERROR";
var message = "Due Date cannot be less then Current Date";
var GetCurrentDate = new Date();
var CurrentDate = GetCurrentDate.getTime().toString();
var GetDueDate = dojo.date.stamp.fromISOString(workItem.getValue(com.ibm.team.workitem.api.common.WorkItemAttributes.DUE_DATE));
var duedate = GetDueDate.getTime().toString();
var PHED = dojo.date.stamp.fromISOString(workItem.getValue("HandoverPlannedEndDate"));
var StringPHED = PHED.getTime().toString();
// Compare the two dates and make sure DueDate is not earlier than CurrentDate
if ((workItem.getValue(WorkItemAttributes.TYPE)=="change_request") && (duedate >= CurrentDate)) {
return Status.OK_STATUS;
}else {
return new Status(Severity[severity], message);
}
}
});
})();
I am using formal process management template and the CLM version is 6.0. Please let me know your thoughts on this. Any help is appreciated. Thanks.
Comments
Donald Nong
Apr 14 '16, 1:05 a.m.Posting a question as an answer to in an old post does not get you anywhere. I've converted it to a question now.
Donald Nong
Apr 14 '16, 1:16 a.m.Your script is quite messy, to say the least. Why do you convert a String to a Date, and then to a String, and finally compare two String values? What is the purpose of the variables PHED and StringPHED? You did not say whether the script works for you or not - have you tried it?
Ralph Schoon
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER Apr 14 '16, 2:33 a.m.Posting on an old question is not a good idea and trying to direct questions in specific users directions neither.