ServiceNow Compare Dates

A quick update on the blog, since you may have notice a change….

I changed the look and layout of the blog for easy viewing and a streamlined look. I think this will be easier to read and therefore much better.

Now on to your regular scheduled programming….

Today we are going to be discussing ServiceNow (SN). I started developing for SN about two months and began to love it right away. I come from a JavaScript background, so I already knew the coding aspect of SN. But I knew nothing about the platform itself.

I will be sharing a code snippet with you that will look similar to JavaScript handles it, but introduce a few SN directives.

This is a script that runs on the client side that compares two dates to ensure the end date is after the start date.

</pre>
function onSubmit(){
//g_form.getValue will get the value on on the form.
//It accepts a string value of the field name.
//This step we are gettin the value from work_start and work_end
//and placing them in a variable.
var startDate = g_form.getValue('work_start');
var endDate = g_form.getValue('work_end');

//g_user_date_time_format is a global variable in service now
//This should already defined, but just in case, we'll check!
if(typeof g_user_date_time_format !== 'undefined') {
//We will use this variable later
var format = g_user_date_time_format;
} else {
return true;
}

//Check to see if there is something defined for dates
if(startDate === "" || endDate === ""){
return true;
}

//Format the dates using the form variable
//getDateFromFormat accepts the date and the format
var startDateMs = getDateFromFormat(startDate, format);
var endDateMs = getDateFromFormat(endDate, format);

//Check if the start is before the end date
if(startDateMs < endDateMs){
return true;
}

//This will clear any messages currently on the SN form
g_form.clearMessages();

//At this point we are checking to make sure there are values for each date
//Then displaying a message on the form that it is invalid.
if(startDateMs === 0 || endDateMs === 0){
if(startDate === 0){
g_form.addErrorMessage(new GwtMessage().getMessage("{0} is invalid", g_form.getLabelOf("work_start")));
}

if(endDate){
g_form.addErrorMessage(new GwtMessage().getMessage("{0} is invalid", g_form.getLabelOf("work_end")));
}

return false;
}

if(startDateMs >= endDateMs){
g_form.addErrorMessage(new GwtMessage().getMessage("{0} must be after {1}", g_form.getLabelOf("work_end"), g_form.getLabelOf("work_start")));

return false;
}
}

I have this living on a change request form, but this can easily be applied to any form where you need to compare two values. This value do not have to be dates, they can really be anything you want, of course you will need to change the code in order to have correct compares.

This is a pretty simple cross between ServiceNow and JavaScript, so a great started. I have many more articles coming, as I find time to post.

About bwilson 36 Articles
Mobile device specialist currently working as a system analyst, building out an IT infrastructure. I currently focus much of my work on relational databases as well as frameworks. The goal of my work is to continually improve processes and efficiencies.

Be the first to comment

Leave a Reply

Your email address will not be published.


*