- 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 12:37 AM
Hi @Lucky1
I tried UI policy but issue is it has only hours
Try to put .25 hours and let see if it work with no code way.
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 12:38 AM
Hello,
Check out below community links, they might be able to help you:
https://www.servicenow.com/community/developer-forum/find-difference-between-two-dates-in-minutes/m-...
If my answer has helped you in any way please mark it correct or helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 12:45 AM
@Lucky1 You can create a client script as given below:
Client Script Details, you can adjust it as per your requirement for the subsequent action to be taken:
Type: onChange
Table: Change Request (change_request)
Field: Start date (start_date) or End date (end_date)
Script: The script will validate the difference between the two dates when either field is updated.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// Ensure the script runs only when the form is not loading
if (isLoading || newValue === '') {
return;
}
// Get the values of Start date and End date fields
var startDate = g_form.getValue('start_date');
var endDate = g_form.getValue('end_date');
// If either field is empty, exit the script
if (!startDate || !endDate) {
return;
}
// Convert the date strings to JavaScript Date objects
var startDateObj = new Date(startDate);
var endDateObj = new Date(endDate);
// Calculate the difference in milliseconds
var timeDifference = endDateObj - startDateObj;
// Convert the difference to minutes
var timeDifferenceInMinutes = timeDifference / (1000 * 60);
// Check if the difference is less than 15 minutes
if (timeDifferenceInMinutes < 15) {
// Show an error message to the user
g_form.showFieldMsg('end_date', 'The difference between Start date and End date must be at least 15 minutes.', 'error');
// Clear the End date field to force the user to correct it
g_form.setValue('end_date', '');
} else {
// Clear any existing error messages
g_form.hideFieldMsg('end_date');
}
}
If my response helped you in anyway please mark it as helpful.
Thank you,
Mahesh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 12:56 AM
you cannot use data policy as it's not possible to use 15mins.
Better you can use the logic during the integration itself.
What type of integration is being used? Import set or Scripted REST API?
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