The CreatorCon Call for Content is officially open! Get started here.

Compare two date/times

Community Alums
Not applicable

Hi everyone,

I have a date/time field called 'Follow Up'. I am setting a rule to block users from selecting a date/time further than 3 months in the future. How can I achieve this using a Business Rule? 

find_real_file.png

 

 

This BR script I have so far but not working, works on After Insert or Update:

var gr = new GlideRecord('incident');
gr.addQuery('sys_id','621ee25bdb917b00cbebaa805b9619d3');
gr.query();

if(gr.next()){

var gdt1 = gr.follow_up;

var time = new GlideDateTime(); // Without input sets it to current UTC.
var gdt2 = time.getDisplayValue(); //Gets the date and time value in the current user's display format and time zone.

var dur = GlideDateTime.subtract(gdt1, gdt2); //the difference between gdt1 and gdt2
gs.print(dur.getDisplayValue());

if (dur >= 90) {
    current.follow_up.setError("Data/time must be within next 3 months");
    current.setAbortAction(true);
}


}
1 ACCEPTED SOLUTION

Community Alums
Not applicable

Thanks everyone for the help.

I am sharing the final code which I tested and is working.

Basically it ensures no date/time further than 3 months in the future is selected and also no date/time in past is selected.

 I used a before insert and update Business Rule.

(function executeRule(current, previous /*null when async*/) {

//Script to ensure no date/time further than 3 months in the future is selected
var gdt1 = new GlideDateTime(current.follow_up);
var gdt2 = new GlideDateTime();
gdt2.addMonthsLocalTime(3);
	
if(gdt1.after(gdt2)) {  // Date time selected is after 3 months 
	gs.addErrorMessage("Follow up date/time cannot be after 3 months.");
	current.follow_up = previous.follow_up;
}

//Script to ensure no date/time in past is selected	
var currentDateTime = new GlideDateTime();	
if(gdt1.before(currentDateTime)) {    // Date time selected in past
	gs.addErrorMessage("Follow up date/time cannot be set in the past.");
	current.follow_up = previous.follow_up; 
}
	
})(current, previous);

View solution in original post

12 REPLIES 12

Community Alums
Not applicable

Thanks for you feedback 🙂

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Jaissica,

why not have this as UI policy instead which won't require any script?

condition : follow up relative on or after 3 months from now

screenshot below; you create UI policy on your table and select proper field

find_real_file.png

in execute if script true add this message; ensure proper field name you give

function onCondition() {
g_form.addErrorMessage('Data/time must be within next 3 months');
g_form.clearValue('follow_up_field');
}

find_real_file.png

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Community Alums
Not applicable

Thanks for you feedback 🙂

Hi Jaissica,

If this is resolved can you also mark my answer as correct and 👍 helpful & then close the thread

Regards

Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

Can you Please give me a proper solution on it .i have same requirement like if today is 12/09/22 then expected date filed should 2 months from today i.e 12/11/22 how i can achieved this. With Scripting or without scripting.