Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

I have two fields in sctask table, one is choice with two options, second is date and…

  1. 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

  2. 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:

    1. Create a Client Script
    2. 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:

    1. Navigate to System Definition > Client Scripts and click on New to create a new Client Script.

    2. 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)
    3. 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.

    1. Navigate to System Definition > UI Policies and click on New to create a UI Policy.

    2. 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
    3. In the UI Policy Actions related list, add a new action:

      • Field name: The Date/Time field
      • Disable: True
      • Ensure the checkbox Reverse if false is checked
    4. Click on Submit to save the UI Policy.

    5. Navigate to the UI Policy Conditions related list and define the conditions:

      • Field: The Choice field
      • Operator: is
      • Value: Option 2 (Put the exact value as per Choice field)
    6. 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.