- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 12:28 AM
Hello all,
On a change request, I want to make sure the difference between Start date and End date to be at least 15 mins but not below that.
It should be applicable during a change request record creation using Integration also.
So, I guess Data policy will work.
So, can someone please give me the code for this scenario.
Regards,
Lucky
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 01:26 AM
Hello @Lucky1 , You can write a Business Rule - configured to run before Insert and Update. you can use below script
(function executeRule(current, previous /*null when async*/) {
var startDate = current.work_start;
var endDate = current.work_end;
var startGDT = startDate.getGlideObject();
var endGDT = endDate.getGlideObject();
var compareTime = 900000; // 15 mins milliseconds
var duration = GlideDateTime.subtract(startGDT, endGDT).getNumericValue();
if (duration < compareTime) {
gs.addErrorMessage("Date difference should be more than 15 mins");
}
})(current, previous);
Regards,
Nishant
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 01:26 AM
Hello @Lucky1 , You can write a Business Rule - configured to run before Insert and Update. you can use below script
(function executeRule(current, previous /*null when async*/) {
var startDate = current.work_start;
var endDate = current.work_end;
var startGDT = startDate.getGlideObject();
var endGDT = endDate.getGlideObject();
var compareTime = 900000; // 15 mins milliseconds
var duration = GlideDateTime.subtract(startGDT, endGDT).getNumericValue();
if (duration < compareTime) {
gs.addErrorMessage("Date difference should be more than 15 mins");
}
})(current, previous);
Regards,
Nishant
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 01:41 AM
Hi Nishant,
But even if we write BR, this logic won't get executed if the record is creating from external source.
Am I right?
Please correct me
Regards,
Lucky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 01:50 AM
business rule will be evaluated even if record is getting created/updated unless somebody adds this condition in BR
gs.getSession().isInteractive()
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 02:10 AM
Hello @Lucky1 , As Ankur confirmed already it'd be executed even for external requests, this approach should satisfy your ask.
Regards,
Nishant
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 02:30 AM
Hi Nishant,
Ok, So, I will go with the BR,
But should I write this line also as Ankur replied
gs.getSession().isInteractive()
Regards,
Lucky