in servicenow, How to restrict Start date time should not be more than 30 minutes from current time ?
Scenario:
Added two fields on catalog item , field1 : Start Date time , Field2 : End Date time
2 .Start date should not be more than 30 minutes from current time .
There should not be less than difference of 60 minutes between start date and end date .
Assistant
To achieve the described restrictions on the "Start Date time" and "End Date time" fields on a ServiceNow catalog item, you can use UI Policies or Client Scripts. Below, I'll provide a solution using Client Scripts to enforce the rules:
Start date should not be more than 30 minutes from the current time.
There should be at least a 60-minute difference between start date and end date.
Here's how you can do it:
Step-by-Step Solution:
Step 1: Create the Catalog Item
Navigate to Maintain Items and create a new catalog item or open an existing one.
Add the two fields for "Start Date time" and "End Date time" to the catalog item.
Step 2: Add Client Scripts
Navigate to Client Scripts and create a new Client Script for your catalog item.
Client Script for Start Date Validation
Name: Validate Start Date Time Table: Your catalog item's table (e.g., sc_cat_item) Type: onChange Field name: Variable containing the "Start Date time" field
Script:
javascript
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Restrict Start Date Time to not be more than 30 minutes from current time
var currentDateTime = new GlideDateTime();
var startDateTime = new GlideDateTime(newValue); // The new start date value
// Calculate difference in minutes
var diffMinutes = (startDateTime.getNumericValue() - currentDateTime.getNumericValue()) / 1000 / 60;
if (diffMinutes > 30) {
g_form.showFieldMsg(control.getName(), 'Start Date/Time cannot be more than 30 minutes from the current time.', 'error');
g_form.clearValue(control.getName());
}
}
Client Script for End Date Validation
Name: Validate End Date Time Table: Your catalog item's table (e.g., sc_cat_item) Type: onChange Field name: Variable containing the "End Date time" field
Script:
javascript
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Ensure there is at least a 60-minute difference between Start Date Time and End Date Time
var startDateTime = g_form.getValue("start_date_time_variable_name"); // Replace with your variable name
var endDateTime = new GlideDateTime(newValue);
if (startDateTime !== '') {
var startDateTimeObj = new GlideDateTime(startDateTime);
// Calculate difference in minutes
var diffMinutes = (endDateTime.getNumericValue() - startDateTimeObj.getNumericValue()) / 1000 / 60;
if (diffMinutes < 60) {
g_form.showFieldMsg(control.getName(), 'End Date/Time must be at least 60 minutes later than Start Date/Time.', 'error');
g_form.clearValue(control.getName());
}
}
}
Explanation:
Validate Start Date Time
The script compares the Start Date Time with the current date and time and ensures the difference is not more than 30 minutes.
If it exceeds 30 minutes, it shows an error message and clears the value.
Validate End Date Time
The script checks that the End Date Time is at least 60 minutes later than the Start Date Time.
If the difference is less than 60 minutes, it shows an error message and clears the value.
Make sure to replace "start_date_time_variable_name" with the actual variable name you've used for the Start Date Time field.
By following these steps and using these scripts, you can successfully enforce the date and time restrictions on your ServiceNow catalog item.