- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2019 03:27 AM
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?
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);
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2019 03:18 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2019 03:15 AM
Thanks for you feedback 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2019 03:43 AM
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
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');
}
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2019 03:15 AM
Thanks for you feedback 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2019 03:16 AM
Hi Jaissica,
If this is resolved can you also mark my answer as ✅ correct and 👍 helpful & then close the thread
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2022 07:17 AM
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.