I have two fields in sctask table, one is choice with two options, second is date and…
- User
I have two fields in sctask table, one is choice with two options, second is date and time field. My requirement is when i select option 2 in choice field and date field has value has today date or lesser than todays date, then i cant be able to select this value or not able to submit the form in servicenow
- Assistant
In ServiceNow, you can achieve this requirement using a combination of client scripts and UI policies. The following steps outline how to implement this:
- Create a Client Script
- Create a UI Policy
Step 1: Create a Client Script
To restrict the submission of the form based on the choice selection and date value, you can create an onSubmit Client Script. Here's how to do it:
-
Navigate to
System Definition > Client Scriptsand click onNewto create a new Client Script. -
Configure the Client Script with the following properties:
- Name: Restrict Submission Based on Choice and Date
- Table:
sc_task - Type: onSubmit
- UI Type: All (or Desktop if specific to Desktop)
-
In the script section, add the following code:
(function executeRule(form) { var choiceField = form.getValue('your_choice_field'); // Replace 'your_choice_field' with actual Choice field name var dateTimeField = form.getValue('your_date_field'); // Replace 'your_date_field' with actual Date/Time field name if (choiceField === 'option2') { // Assuming 'option2' is the value you are checking var date = new Date(dateTimeField); var today = new Date(); today.setHours(0, 0, 0, 0); // Reset to midnight to only compare date part date.setHours(0, 0, 0, 0); if (date <= today) { alert('You cannot select this date/time for Option 2.'); return false; } } return true; })(g_form);
Step 2: Create a UI Policy
To restrict the selection of date/time for Option 2 dynamically on the form without submitting, you can create a UI Policy to perform the check.
-
Navigate to
System Definition > UI Policiesand click onNewto create a UI Policy. -
Configure the UI Policy with the following properties:
- Name: Restrict Date Selection Based on Choice
- Table:
sc_task - Active: True
- Global: True or limit to specific views
-
In the
UI Policy Actionsrelated list, add a new action:- Field name: The Date/Time field
- Disable: True
- Ensure the checkbox
Reverse if falseis checked
-
Click on Submit to save the UI Policy.
-
Navigate to the
UI Policy Conditionsrelated list and define the conditions:- Field: The
Choicefield - Operator: is
- Value: Option 2 (Put the exact value as per
Choicefield)
- Field: The
-
Add an additional condition:
- Click "New Criterion" (if your version supports multiple conditions)
- Field: Date/Time Field
- Operator: on or before
- Value:
javascript:gs.daysAgoStart(0)(will put today's start date)
Finalizing
Save and test the form. Make sure the Client Script and UI Policy are working together by:
- Disabling the Date/Time field when Option 2 is selected and the date is today or before.
- Preventing form submission with an alert if the user tries to submit with an invalid state.
With these scripts and policies, you should be able to meet the requirement of disabling the Date/Time field and preventing submission of the form based on the specified conditions.