- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2017 09:37 AM
I need an 'OnChange' script that will check to see whether the entered 'start_date' that is less than 2 weeks from today (or whatever day they are filling out the form). If they enter a date less than 2 weeks away, I want to display a pop up message and prevent them from submitting the form.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2017 05:22 AM
Hi Nicole,
If you don't want to do ajax calls each time, you can use JavaScript validation in onChange client script and onSubmit script :
var date1 = new Date(g_form.getValue('start_date');
var date2 = new Date();
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
if(diffDays >= 14) {
alert("Error Message");
return false;
}
else
return true;
Regards,
Chirag Bagdai
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2017 10:08 AM
I've updated Business Rule:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var gdt = new GlideDateTime(gs.nowDateTime());
gdt.addDays(-14); // two week ago
g_scratchpad.current_date_user_timezone = gdt.getDate().toString();
})(current, previous);
And client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var date1 = new Date(g_scratchpad.current_date_user_timezone);
var date2 = new Date(g_form.getValue('start_date'));
if(date1 > date2) {
g_form.clearValue('start_date');
alert("You've entered a start date less than two weeks prior to the Start Date of the course. We cannot support all of the logistical requirements to ensure a successful delivery for this course.");
return false;
}
else
return true;
}
Still no message displays.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2017 10:22 AM
Hi Nicole,
I checked the same business rule and client script for incident table in my instance and it's working fine. I think, there is something else which is causing the problem.
Can you please share the screenshot of business rule and client script ?
Regards,
Chirag
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2017 10:43 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2017 11:05 AM
Thanks for sharing the screenshots.
I tested for incident table but since you are trying for catalog item so unfortunately, this approach won't work due business rule. Let me try another approach and get back to you.
Regards,
Chirag
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2017 11:53 AM
Hi Nicole,
As per your requirement - I think, the best solution is to use GlideAjax as describe below :
I have created client callable script include (StartDateValidation).
//Client Callable Script Include
var StartDateValidation = Class.create();
StartDateValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateDateStartDate : function() {
var input_start_date = this.getParameter('sysparm_start_date');
if(input_start_date) {
input_start_date = new GlideDateTime(input_start_date);
var system_date = new GlideDateTime(gs.nowDateTime());
system_date.addDays(-14);
if(system_date.getDate() > input_start_date)
return false;
else
return true;
}
return false;
},
type: 'StartDateValidation'
});
//Client Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//onChange Client Script
if(newValue != '') {
var ga_date_validation = new GlideAjax('global.StartDateValidation');
ga_date_validation.addParam('sysparm_name','validateDateStartDate');
ga_date_validation.addParam('sysparm_start_date', newValue);
ga_date_validation.getXML(startDateValidationResponse);
}
}
function startDateValidationResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == "false") {
g_form.setValue('start_date','');
alert("You've entered a start date less than two weeks prior to the Start Date of the course. We cannot support all of the logistical requirements to ensure a successful delivery for this course.");
}
}
Please test above script and let me know in-case of any issue.
Regards,
Chirag