
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
This post covers few basic use cases related to dates mostly used in Service Catalog or Change Management.
I have seen a lot of questions in community, where we want to validate a date or we want to default a date field to a future date.
I have gathered few such scenarios and tried to provide solutions with the help of community answers and my experience.
Most of them are using GlideAjax and onChange Client Script.
If you want to perform validation while record submission as well, you should use them in an onSubmit Script.
Note: While using them in an onSubmit script, use getXMLWait() instead of a callback function. Because an onSubmit will not wait for a callback since callbacks are asynchronous.
If you don't want to write another onSubmit script, you can set the field value blank when validation fails by using g_form.setValue('start_date',''), so that user must enter a another date.
All the client scripts are doing a GlideAjax call to a script include 'DateTimeFunc'. Make sure you set the UI Type to All in client script to make it work in Service Portal.
Following are the functions defined in the script include.
1. addTime: If you want to add time to your end date based on start date selection
2. addTimeSchedule: If you want to add time to your end date, but the end date shouldn't fall during weekends or off hours. The schedule must be there is cmn_schedule table.
3. ValidateLeadTime: If you want to validate a lead time. For example, most of the organizations wants a lead time of 2 weeks for a comprehensive change. So while submitting a change, requester should choose a planned start date which is after 2 weeks from today.
4. CompareDates: When you want to compare any two dates and find out which is greater
5. ValidateBlackOuts: When you want your request or change implementation date doesn't fall in a blackout window.
I will keep improving the script based on your feedback and suggestions and add any other use cases related to dates which are used frequently.
Use Case 1: I want to set a default date
Set default value of the variable (Catalog Item) or field (Table) to below.
Add 14 days to current date
javascript:var cdt = new GlideDateTime();cdt.addDays(14);cdt.getDisplayValue();
Set date to current date with time '12:00:00'
javascript:var cdt = new GlideDateTime();cdt.getDisplayValue().split(' ')[0] + ' 12:00:00';
Use Case 2: I want to set end date based on start date
Script Type: Client Script
Type: OnChange
Variable/Field Name: start_date
UI Type: All
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('DateTimeFunc');
ga.addParam('sysparm_name', 'addTime');
ga.addParam('start_date', newValue);
ga.addParam('time_to_add', '86400'); // Add 24 hours
ga.getXML(getEndDate);
}
function getEndDate(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('end_date',answer);
}
Use Case 2.1: I want to set end date based on a Schedule
Script Type: Client Script
Type: OnChange
Variable/Field Name: start_date
UI Type: All
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('DateTimeFunc');
ga.addParam('sysparm_name', 'addTimeSchedule');
ga.addParam('start_date', newValue);
ga.addParam('time_to_add', '86400'); // Add 24 hours. If it is a 9*5 schedule (8 hrs per day), it will add 3 business days to start date
ga.addParam('schedule','8-5 weekdays'); // You must have a record of the schedule in cmn_schedule table
ga.getXML(getEndDate);
}
function getEndDate(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('end_date',answer);
}
Use Case 3: I want a Lead Time validation for date entered by requestor
Script Type: Client Script
Type: OnChange
Variable/Field Name: start_date
UI Type: All
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.hideFieldMsg('start_date');
var start_date = g_form.getValue('start_date');
var ajax = new GlideAjax('DateTimeFunc');
ajax.addParam('sysparm_name', 'ValidateLeadTime');
ajax.addParam('start_date', start_date);
ajax.getXML(validateDate);
}
function validateDate(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer==1)
g_form.showFieldMsg('start_date',"The start date can't be in past",'error');
else if (answer==2)
g_form.showFieldMsg('start_date',"Please consider at least 2 weeks of lead time",'error');
}
Use Case 4: I want to Validate Start Date < End Date
Script Type: Client Script
Type: OnChange
Variable/Field Name: end_date
UI Type: All
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.hideFieldMsg('end_date');
var ga = new GlideAjax('DateTimeFunc');
ga.addParam('sysparm_name', 'compareDates');
ga.addParam('start_date', g_form.getValue('start_date'));
ga.addParam('end_date', g_form.getValue('end_date'));
ga.getXML(getEndDate);
}
function getEndDate(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer=='true')
g_form.showFieldMsg('end_date','End date should be after start date','error');
}
Use Case 5: Date requested shouldn't fall in Blackout Windows
Script Type: Client Script
Type: OnChange
Variable/Field Name: start_date
UI Type: All
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.hideFieldMsg('start_date');
var ga = new GlideAjax('DateTimeFunc');
ga.addParam('sysparm_name', 'ValidateBlackOuts');
ga.addParam('start_date', newValue); //Pass the date you want to validate
ga.getXML(isBlackout);
}
function isBlackout(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer=='true')
g_form.showFieldMsg('start_date','Please select a date outside blackout window','error');
}
Script Include
Name : DateTimeFunc
Client Callable: true
Application: Global
var DateTimeFunc = Class.create();
DateTimeFunc.prototype = Object.extendsObject(AbstractAjaxProcessor, {
addTime: function() {
// Get the Start Date
var start = this.getParameter('start_date');
var timeToAdd = this.getParameter('time_to_add');
var gdt = new GlideDateTime();
gdt.setDisplayValue(start);
//add time to Start Date
gdt.addSeconds(timeToAdd);
//Return the End Date to Client Script
return gdt.getDisplayValue();
},
addTimeSchedule: function() {
var start = this.getParameter('start_date');
var timeToAdd = this.getParameter('time_to_add');
var sch = this.getParameter('schedule');
var gdt = new GlideDateTime();
gdt.setDisplayValue(start);
//Get a schedule by name to calculate duration
var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('name', sch);
if (typeof GlideSchedule != 'undefined')
var sched = new GlideSchedule(schedRec.sys_id);
else
var sched = new Packages.com.glide.schedules.Schedule(schedRec.sys_id);
//Set the amount of time to add (in seconds)
durToAdd = new GlideDuration(timeToAdd*1000);
var newDateTime = sched.add(gdt, durToAdd, '');
//Return the new date
return newDateTime.getDisplayValue();
},
ValidateLeadTime: function() {
var se_start_date = this.getParameter('start_date');
var opened_date = gs.nowDateTime();
var currentDateTime = new GlideDateTime();
currentDateTime.setDisplayValue(opened_date);
var start_date = new GlideDateTime();
start_date.setDisplayValue(se_start_date);
if (se_start_date!='' && start_date<currentDateTime)
{
return 1; // Start Date Entered is in past
}
else if (se_start_date!='')
{
var dc = new DurationCalculator();
dc.setStartDateTime(currentDateTime);
if (!dc.calcDuration(14*24*3600)) // Add 2 weeks
gs.log("*** Error calculating duration");
var newDateTime = dc.getEndDateTime();
if (start_date < newDateTime)
{
return 2; // Date entered is within the 2 weeks of lead time
}
}
},
compareDates: function() {
var chg_start_date = this.getParameter('start_date');
var chg_end_date = this.getParameter('end_date');
if (chg_start_date > chg_end_date)
{
return true;
}
return false;
},
ValidateBlackOuts: function() {
var ch_start_date = this.getParameter('start_date');
var start_date = new GlideDateTime();
start_date.setDisplayValue(ch_start_date);
var g = new GlideRecord('cmn_schedule');
var q = g.addQuery('name', 'Year-End Freeze');
q.addOrCondition('name','IT Infrastructure Blackouts');
g.query();
while (g.next()) {
var sched = new GlideSchedule(g.sys_id);
if (sched.isInSchedule(start_date)){
return true;
}
}
return false;
},
});
- 33,669 Views
- « Previous
-
- 1
- 2
- 3
- 6
- Next »
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.