- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2023 07:53 AM
Hello. Recently I'm working on an adjustment that envolve comparission two dates. A specific catalog item should not allow any user to open a new request via service portal, if the current date is bigger than 24th day of the current month.
First, I'd created this validation on a client script, but testing I have identified that if date/time of users devices are not equal of the current date/time (for example, not automatic on windows), this code could fail on his validation.
var today = new Date();
if (today.getDate() >24) {
g_form.addErrorMessage('Data Limite para abertura desta solicitação excedida.');
return false;
So now, I need to create a GlideAjax and get the server date, to compare and validate if the current date is bigger de 24th.
Does anyone had developed something similar to this? Or know how it could work?
Best regards,
Alberto.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2023 11:45 AM
Here is an example of how you can get the server date via a Script Include and compare it to another date in a Client Script in ServiceNow:
1. Script Include:
var ServerDate = Class.create();
ServerDate.prototype = {
initialize: function() {
},
getDate: function() {
var gr = new GlideRecord('sys_calendar');
gr.get("sys_id", "1");
var d = gr.getValue('current');
return d;
}
};
2. Client Script:
var targetDate = new GlideDate();
targetDate.setValue("2023-02-07");
var serverDate = new ServerDate().getDate();
if (targetDate.compareTo(serverDate) >= 0) {
// targetDate is greater than or equal to serverDate
gs.addInfoMessage("The target date is greater than or equal to the server date");
} else {
// targetDate is less than serverDate
gs.addInfoMessage("The target date is less than the server date");
}
Note: The ServerDate Script Include and the Client Script should be added to your ServiceNow instance via the UI.
You can Thanks me Here: Alight motion Pc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2023 09:01 AM
Hello,
I would suggest that you slightly change the approach - instead of returning the server date and doing evaluation client side, just the date user has entered and do the comparison server side.
in script include you will have something like this
var retVal = 'false'; // Return value
var planned = this.getParameter('sysparm_planned');
var now = gs.nowDateTime();
var difference = gs.dateDiff(now, planned, true);
var days = ((parseInt(difference)/60)/60);
if (parseInt(days) <= 24){
retVal = 'true';
}
return retVal;
If needed, I can share the client side part as well.
Best regards,
Boyan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2023 09:04 AM
Hello,
I would suggest that you do the whole validation server side. In GlideAjax call you can pass the input from the user and check it in the script include.
Script include code will look like this
var retVal = 'false'; // Return value
var planned = this.getParameter('sysparm_planned');
var now = gs.nowDateTime();
var difference = gs.dateDiff(now, planned, true);
var days = ((parseInt(difference)/60)/60);
if (parseInt(days) <= 24){
retVal = 'true';
}
return retVal;
Let me know if I shall share the client side script as well.
Best regards,
Boyan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2023 11:02 AM
if the current date is bigger than 24th day of the current month
In which time zone?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2023 11:45 AM
Here is an example of how you can get the server date via a Script Include and compare it to another date in a Client Script in ServiceNow:
1. Script Include:
var ServerDate = Class.create();
ServerDate.prototype = {
initialize: function() {
},
getDate: function() {
var gr = new GlideRecord('sys_calendar');
gr.get("sys_id", "1");
var d = gr.getValue('current');
return d;
}
};
2. Client Script:
var targetDate = new GlideDate();
targetDate.setValue("2023-02-07");
var serverDate = new ServerDate().getDate();
if (targetDate.compareTo(serverDate) >= 0) {
// targetDate is greater than or equal to serverDate
gs.addInfoMessage("The target date is greater than or equal to the server date");
} else {
// targetDate is less than serverDate
gs.addInfoMessage("The target date is less than the server date");
}
Note: The ServerDate Script Include and the Client Script should be added to your ServiceNow instance via the UI.
You can Thanks me Here: Alight motion Pc