Future Date Validation with current date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2022 12:45 PM
Hi Everyone!!!
I have a requirement related to Date Validation, Where I have a Date Field (end date) in user table and i need to check the end date is 14 days equal current date ( current date : 17/09/22 then end date is :31 /09/22) in the user table. i need to run the script everyday so i have created the the schedule job to check it but it's not working.
var gr = new GlideRecord('u_my_user_details');
gr.addEncodedQuery('end_date='current.u_end_date);
gr.query();
if (gr.next()) {
var futureDate = new GlideDateTime();
futureDate.addDaysLocalTime(14);
var dateDiffCalc = gs.dateDiff(current.u_end_date.getDisplayValue(),futureDate.getDisplayValue(), true);
if (current.u_end_date && dateDiffCalc > 0)
gs.addinfoMessage('hello');
}
- Labels:
-
Incident Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2022 02:15 PM
>>> To help others, please mark this as Correct or Helpful if this response has been of any use. <<<
Hi Sushma,
Your encoded query does not look correct. What is current.u_end_date? You could debug this script using scripts background to better understand what is happening.
var gr = new GlideRecord('u_my_user_details');
gr.addEncodedQuery('end_date='current.u_end_date);
gr.query();
if (gr.next()) {
var futureDate = new GlideDateTime();
futureDate.addDaysLocalTime(14);
var dateDiffCalc = gs.dateDiff(current.u_end_date.getDisplayValue(),futureDate.getDisplayValue(), true);
if (current.u_end_date && dateDiffCalc > 0)
gs.addinfoMessage('hello');
}
>>> To help others, please mark this as Correct or Helpful if this response has been of any use. <<<
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2022 11:37 PM
Hello Sushma,
If you are looking to fetch the record from User table whose end date is 14 days ahead from todays date then I believe you can use the below script in your scheduled Job:
(function () {
var todayDateGDT = new GlideDateTime();
todayDateGDT.addDays(14);
var actualEndDate = todayDateGDT.getDate();
var query = "u_end_dateON" + actualEndDate +"@javascript:gs.dateGenerate('" + actualEndDate + "','start')@javascript:gs.dateGenerate('" + actualEndDate + "','end')";
var myUserGR = new GlideRecord("u_my_user_details");
myUserGR.addEncodedQuery(query);
while (myUserGR.next()) {
//Write your code here
}
})();
If my answer helped you in any way then please do mark it as helpful. If it answered your question then please mark it correct and helpful. This will help others with similar issue to find the correct answer.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2022 11:02 PM
Hello Sushma,
Just wanted to check with you, if the above response answered your question. If yes, then please do close this thread/question by marking the appropriate response as correct.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2022 11:56 PM
Hi,
I think no need any scripting required for this if you are validating end date after 14 days.
It will work only when end is 14 days from current date.
Set filter with your end date and give 14 days from now and run.
Then right on query and copy query use it on your addEncodedQuery in script.
No need to check with in if loop again if end date >14 days from now It will get those.
refer below screenshot.
Hope you it helps you.
Please Mark ✅ Correct/helpful if applicable, Thanks!!
Regards
Pavankumar
ServiceNow Community MVP 2024.
Thanks,
Pavankumar