It's all about the answers!

Ask a question

How the below code works for Date Validation


Arjun Pande (3814) | asked Apr 14 '16, 1:33 a.m.
edited Apr 15 '16, 8:10 a.m. by Ralph Schoon (63.6k33646)


Note: the user deleted the content of the question. Please see revision history to see the content.



      String. Is there any alternate method?

Accepted answer


permanent link
Ralph Schoon (63.6k33646) | answered Apr 14 '16, 2:03 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
edited Apr 14 '16, 2:08 a.m.
This is not at all a RTC API Question, this is a JavaScript question and you should likely ask Google and not this forum. Stackoverflow and other language related forums usually provide hints if you just search for e.g. in this case "JavaScript date compare".

Here an example script I created after searching the internet:

/*******************************************************************************
 * 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("org.example.DateValidator");

dojo.require("com.ibm.team.workitem.api.common.Severity");
dojo.require("com.ibm.team.workitem.api.common.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

/*
 *
 * You need to configure this script in the Process Configuration Source:
 *     <validator id="dateval" name="Date Validator" providerId="com.ibm.team.workitem.shared.common.internal.valueProviders.ScriptAttributeValueProvider">
 *         <script class="org.example.DateValidator" path="/workitem/scripts/common/DateValidator.js"/>
 *         <parameters beginDateAttributeId="startdate" message="The completion date can not be earlier than the start date" severity="ERROR"/>
 *     </validator>
 *
 */
(function() {
var Severity = com.ibm.team.workitem.api.common.Severity;
var Status = com.ibm.team.workitem.api.common.Status;

var DateValidator = dojo.declare("org.example.DateValidator", null, {

    validate: function(attributeId, workItem, configuration) {
   
        // Get the configuration parameters about the severity and error message of this validator
        var severity= configuration.getChild("parameters").getStringDefault("severity", Severity.ERROR.name);
        var message= configuration.getChild("parameters").getStringDefault("message", "");
       
        // Get the begin date attribute from the configuration and make a Date object from it
        var beginDateId = configuration.getChild("parameters").getStringDefault("beginDateAttributeId", "");
        var beginDate = dojo.date.stamp.fromISOString(workItem.getValue(beginDateId));
       
        // Get the current attribute's value and make a Date object from it
        var endDate= dojo.date.stamp.fromISOString(workItem.getValue(attributeId));
       
        // Compare the two dates and make sure endDate is not earlier than beginDate
        if (dojo.date.compare(endDate, beginDate) >= 0) {
            return Status.OK_STATUS;
        } else {
            return new Status(Severity[severity], message);
        }
    }
});
})();
Arjun Pande selected this answer as the correct answer

Comments
Arjun Pande commented Apr 14 '16, 2:33 a.m. | edited Apr 14 '16, 2:42 a.m.

Thanks for the quick response Ralph. I am actually trying to compare Due Date (in built attribute of IBM) and System Date where a user if enters a due date prior to current date it should populate a message stating that "Due Date cannot be less than the current date".


Ralph Schoon commented Apr 14 '16, 2:51 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Consider using comments instead of answers if you comment.

It does not matter which attribute. There are two time related attribute types. Duration and Timestamp. Due Date is a Timestamp as used in the code above. You can compare same attribute types. Duration is ticks in milliseconds.


Arjun Pande commented Apr 14 '16, 7:26 a.m.

Sure Ralph,

I am trying to add the below dojo script which is not working for me. Can you please help me where I am going wrong?. I am trying to compare dueDate field of RTC and the date of my system


Arjun Pande commented Apr 14 '16, 7:26 a.m.

dojo.provide("DueDateValidation");

 dojo.require("com.ibm.team.workitem.tab.customAttributes"); // To access the work item attributes
 dojo.require("com.ibm.team.workitem.attribute.state"); // To create a status object to return
 dojo.require("com.ibm.team.workitem.attribute.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


 


Arjun Pande commented Apr 14 '16, 7:26 a.m.

(function() {
 
 var Status = com.ibm.team.workitem.attribute.state;
 var WorkItemAttributes = com.ibm.team.workitem.tab.customAttributes;

 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.attribute.duedate));
     var duedate = GetDueDate.getTime().toString();
     

              
        


Arjun Pande commented Apr 14 '16, 7:26 a.m.

// Compare the two dates and make sure DueDate is not earlier than CurrentDate
         if ((duedate >= CurrentDate)) {
             return Status.OK_STATUS;
         }else {
             return new Status(Severity[severity], message);
         }
     }
 });
 })();


Ralph Schoon commented Apr 14 '16, 7:35 a.m. | edited Apr 14 '16, 7:39 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

I answered with a script that works and a reason why. A main point with answers is to read them and consider their content and not to ask the same question again.


Donald Nong commented Apr 14 '16, 11:33 p.m.

@jainkapil07 You really need to learn how to debug your code, and don't expect others to tell you what's wrong with the code by just reading it.
https://jazz.net/library/article/1360

showing 5 of 8 show 3 more comments

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.