Future Date Validation with current date

sushma9
Tera Contributor

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');
}
 

4 REPLIES 4

Jon23
Mega Sage

>>> 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. <<<

Mahendra RC
Mega Sage

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

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

Pavankumar_1
Mega Patron

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.

find_real_file.png

 

Hope you it helps you.

Please Mark Correct/helpful if applicable, Thanks!! 

 

Regards

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar