-
-
Save wave-inguane/84be96f040d5ce9a8d7d65f3fc63d8eb to your computer and use it in GitHub Desktop.
Input validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on server side if you use .getDisplayValue() on any field, that gives you date time in user's time zone and format. | |
.................................................................................................................. | |
(function executeRule(current, previous /*null when async*/) { | |
try { | |
var poc = current.poc.toString(); | |
var author = current.opened_by.toString(); | |
gs.eventQueue("event", current, current.opened_by, "");//Send to opened by | |
if(poc != author) | |
gs.eventQueue("event", current, current.poc, ""); | |
var gdt = new GlideDateTime(); | |
current.last_sent = gdt.getDisplayValue(); //gdt.getValue(); | |
current.sent_second_email = false; | |
} catch (ex) { | |
} | |
})(current, previous); | |
isDateToday: function(strDate) { | |
// Create GlideDateTime objects for the current date and the passed in date | |
var rightnow = new GlideDateTime(); | |
var testdate = new GlideDateTime(strDate); | |
testdate.addDaysLocalTime(1); | |
// Get the date portion of rightnow and testdate (no timestamp) | |
var today = rightnow.getLocalDate(); | |
var istoday = testdate.getLocalDate(); | |
// Compare today and istoday to see if they are the same day | |
if (today.compareTo(istoday) == 0) { | |
return true; | |
} else { | |
return false; | |
} | |
}, | |
Test in Xplore | |
var dt1 = new GlideDateTime('2021-09-16 09:00:00'); | |
var dt2 = new GlideDateTime('2021-09-16 16:38:00'); | |
var diff = GlideDateTime.subtract(dt1,dt2); | |
//diff | |
//diff "1970-01-01 11:31:40" | |
/* | |
diff = diff.getHourLocalTime(); | |
diff = diff.getHourOfDayLocalTime(); | |
diff = diff.getHourOfDayUTC(); | |
diff = diff.getHourUTC(); | |
*/ | |
//diff = diff.getHourLocalTime(); //3 | |
//diff = diff.getHourOfDayLocalTime(); | |
diff = diff.getHourOfDayUTC(); | |
//diff = diff.getHourUTC(); | |
var gdt = new GlideDateTime("2011-08-31 08:00:00"); | |
var gtime1 = new GlideTime(); | |
gtime1.setValue("00:00:20"); | |
gdt.subtract(gtime1); | |
var gtime2 = gdt.getTime(); | |
gs.info(gtime2.getByFormat('hh:mm:ss')); | |
var gdt1 = new GlideDateTime("2011-08-28 09:00:00"); | |
var gdt2 = new GlideDateTime("2011-08-31 08:00:00"); | |
var dur = GlideDateTime.subtract(gdt1, gdt2); //the difference between gdt1 and gdt2 | |
gs.info(dur.getDisplayValue()); | |
var gdt1 = new GlideDateTime("2016-05-09 10:11:12"); | |
var gdt2 = new GlideDateTime("2017-06-12 15:11:12"); | |
gs.info(gdt1.after(gdt2)); | |
var gdt1 = new GlideDateTime("2016-05-09 10:11:12"); | |
var gdt2 = new GlideDateTime("2017-06-12 15:11:12"); | |
gs.info(gdt1.before(gdt2)); | |
var initDate = new GlideDateTime("2011-08-01 12:00:00"); | |
var compDate1 = new GlideDateTime("2011-08-01 12:00:00"); | |
var compDate2 = new GlideDateTime("2011-07-31 12:00:00"); | |
var compDate3 = new GlideDateTime("2011-08-04 16:00:00"); | |
gs.info(initDate.compareTo(compDate1)); // Equals (0) | |
gs.info(initDate.compareTo(compDate2)); // initDate is after compDate2 (1) | |
gs.info(initDate.compareTo(compDate3)); // initDate is before compDate3 (-1) | |
var gdt = new GlideDateTime("2014-08-31 08:00:00"); | |
gt = gdt.getLocalTime(); | |
gs.info("local time is " + gt.getByFormat('hh:mm:ss')); | |
var gdt1 = new GlideDateTime("2016-05-09 10:11:12"); | |
var gdt2 = new GlideDateTime("2017-06-12 15:11:12"); | |
gs.info(gdt1.onOrAfter(gdt2)); | |
var gdt1 = new GlideDateTime("2016-05-09 10:11:12"); | |
var gdt2 = new GlideDateTime("2017-06-12 15:11:12"); | |
gs.info(gdt1.onOrBefore(gdt2)); | |
/*************************************************************************************************************************************** | |
* | |
**************************************************************************************************************************************** | |
1) Updated between 12 and 24 hours ago | |
current.sys_updated_by != current.assigned_to.user_name // User that updated the record is not the assigned to user | |
&& new GlideDateTime(current.sys_updated_on) < gs.hoursAgo(12) // Record was updated more than 12 hours ago | |
&& new GlideDateTime(current.sys_updated_on) >= gs.hoursAgo(24) // Record was not updated more than 24 hours ago | |
&& current.state != 7 && current.state != 8 // State is not 7 or 8 | |
2 Updated more than 36 hours ago | |
current.sys_updated_by != current.assigned_to.user_name | |
&& new GlideDateTime(current.sys_updated_on) < gs.hoursAgo(36) | |
&& current.state != 7 | |
&& current.state != 8 | |
&& current.state != 6 | |
3 Updated between 24 and 36 hours ago | |
current.sys_updated_by!= current.assigned_to.user_name | |
&& new GlideDateTime(current.sys_updated_on) < gs.hoursAgo(24) | |
&& new GlideDateTime(current.sys_updated_on) >= gs.hoursAgo(36) | |
&& current.state!=7&¤t.stat !=8&¤t.state !=6 | |
/*************************************************************************************************************************************** | |
* DATE VALIDATION | |
**************************************************************************************************************************************** | |
Script Include: NeedItUtils | |
Client callable : false | |
Usage : used by a business rule: NeedIt When needed field date | |
Requirement: | |
The date has to be in the Future | |
var NeedItUtils = Class.create(); | |
NeedItUtils.prototype = { | |
initialize: function() { | |
}, | |
isDatePast: function(strDate){ | |
// Create GlideDateTime objects for the current date and the passed in date | |
var rightnow = new GlideDateTime(); | |
var testdate = new GlideDateTime(strDate); | |
// If the testdate is before rightnow, return true, else return false | |
if (testdate.before(rightnow)) { | |
return true; | |
} | |
else { | |
return false; | |
} | |
}, | |
isDateToday: function(strDate){ | |
// Create GlideDateTime objects for the current date and the passed in date | |
var rightnow = new GlideDateTime(); | |
var testdate = new GlideDateTime(strDate); | |
// Get the date portion of rightnow and testdate (no timestamp) | |
var today = rightnow.getLocalDate(); | |
var istoday = testdate.getLocalDate(); | |
// Compare today and istoday to see if they are the same day | |
if(today.compareTo(istoday) == 0){ | |
return true; | |
} | |
else { | |
return false; | |
} | |
}, | |
type: 'NeedItUtils' | |
}; | |
Busine Rule: NeedIt When needed field date | |
Before : Insert | |
(function executeRule(current, previous /*null when async*/ ) { | |
// Instantiate the NeedItUtils class. Call the isDatePast method and pass | |
// the u_when_needed value. | |
var niutils = new NeedItUtils(); | |
var isPast = niutils.isDatePast(current.u_when_needed); | |
// If the isDatePast method returns true, the date is in the past. | |
if (isPast == true) { | |
gs.addErrorMessage("When needed date cannot be in the past. Your request has not been saved to the database."); | |
current.setAbortAction(true); | |
} | |
// pass the When needed field value to the isDateToday method in NeedItUtils | |
var isToday = niutils.isDateToday(current.u_when_needed); | |
// if the isDateToday method returns true the When needed date is today | |
if (isToday == true) { | |
gs.addErrorMessage("You cannot submit NeedIt requests for today."); | |
current.setAbortAction(true); | |
} | |
})(current, previous); | |
/*************************************************************************************************************************************** | |
* EMAIL VALIDATION | |
**************************************************************************************************************************************** | |
Script Include: validateEmailAddress | |
Client callable : false | |
Usage : used by a business rule: Email Address Syntax Validate | |
function validateEmailAddress(emailStr){ | |
// Use JavaScript coercion to guarantee emailStr is a string | |
emailStr = emailStr + ''; | |
// Compare emailStr against the allowed syntax as specified in the regular expression | |
// If emailStr has allowed syntax, return true, else return false | |
if(emailStr.match(/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)){ | |
return true; | |
} | |
else return false; | |
} | |
Busines Rule: Email Address Syntax Validate | |
Before : Insert or Update | |
(function executeRule(current, previous /*null when async*/) { | |
// Pass the Requested for email to the Script Include. Store the return | |
// value from the Script Include in the isEmail variable | |
var isEmail = validateEmailAddress(current.u_requested_for_email); | |
// If isEmail is false (email address syntax is not valid) do not save | |
// the record. Write an error message to the screen. | |
if(isEmail == false){ | |
gs.addErrorMessage(current.u_requested_for_email + " is not a valid email address. You must provide a valid email address."); | |
current.setAbortAction(true); | |
} | |
})(current, previous); | |
function validateEmailAddress(emailStr){ | |
// Use JavaScript coercion to guarantee emailStr is a string | |
emailStr = emailStr + ''; | |
// Compare emailStr against the allowed syntax as specified in the regular expression | |
// If emailStr has allowed syntax, return true, else return false | |
if(emailStr.match(/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)){ | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
// Pass the Requested for email to the Script Include. Store the return | |
// value from the Script Include in the isEmail variable | |
var isEmail = validateEmailAddress(current.u_requested_for_email); | |
// If isEmail is false (email address syntax is not valid) do not save | |
// the record. Write an error message to the screen. | |
if(isEmail == false){ | |
gs.addErrorMessage(current.u_requested_for_email + " is not a valid email address. You must provide a valid email address."); | |
current.setAbortAction(true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment