How to compare two tables in client script?

Tippireddy Venk
Tera Contributor

When opening a Ctask or editing planned and actual Ctask dates, check Ctask values against CR Planned start and end dates.

If a Ctask planned start or actual start date is prior to the related CR planned start date, display a Pop-up with a warning that the start date is prior to the CR planned start date.

If a Ctask planned end or actual end date is after the related CR planned end date, display a Pop-up with a warning that the end date is after the CR planned end date and that a time extension approval will be required from Change Management.

1 ACCEPTED SOLUTION

pranita-24
Giga Guru

Hi @Tippireddy Venk 
To achieve the described functionality in ServiceNow, you can use Business Rules and Client Scripts

Create a Business Rule:

  1. Navigate to "System Definition" -> "Business Rules" in the application navigator.
  2. Click on "New" to create a new business rule.
  3. Name your business rule, e.g., "CheckCtaskDatesAgainstCRDates".
  4. Set the "Table" to "Change Task [change_task]" or your corresponding table.
  5. Set the "When to run" to "Before" and "Insert", "Update", and "Delete".
  6. Write a script to check the conditions and display a warning message


    (function executeRule(current, previous /*null when async*/) {
    // Check if the planned start date is prior to CR planned start date
    if (current.planned_start < current.change_request.planned_start) {
    gs.
    addErrorMessage("Warning: Ctask start date is prior to CR planned start date.");
    }
    // Check if the planned end date is after CR planned end date
    if (current.planned_end > current.change_request.planned_end) {
    gs.
    addErrorMessage("Warning: Ctask end date is after CR planned end date. Time extension approval will be required from Change Management.");
    }

    })(current, previous);

    2. Create a Client Script:

    1. Navigate to "System Definition" -> "Client Scripts" in the application navigator.
    2. Click on "New" to create a new client script.
    3. Name your client script, e.g., "CheckCtaskDatesClientScript".
    4. Set the "Table" to "Change Task [change_task]" or your corresponding table.
    5. Set the "Type" to "OnChange" for both planned start and planned end fields.
    6. Write a script to check the conditions and display a warning pop-up.

      function onChange(control, oldValue, newValue, isLoading, isTemplate) {
      // Check if the planned start date is prior to CR planned start date
      if (newValue < g_form.getValue('change_request.planned_start')) {
      alert("Warning: Ctask start date is prior to CR planned start date.");
      g_form.setValue('planned_start', oldValue); // Revert to the old value
      }

      // Check if the planned end date is after CR planned end date
      if (newValue > g_form.getValue('change_request.planned_end')) {
      alert("Warning: Ctask end date is after CR planned end date. Time extension approval will be required from Change Management.");
      g_form.setValue('planned_end', oldValue); // Revert to the old value
      }
      }


      I hope this will help you to get your requirement.

      Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
      Thanks





View solution in original post

2 REPLIES 2

Aman Kumar S
Kilo Patron

Hi @Tippireddy Venk 

You can always use glideajax to be able to use server side script to fetch and compare the field values across different tables

 

Best Regards
Aman Kumar

pranita-24
Giga Guru

Hi @Tippireddy Venk 
To achieve the described functionality in ServiceNow, you can use Business Rules and Client Scripts

Create a Business Rule:

  1. Navigate to "System Definition" -> "Business Rules" in the application navigator.
  2. Click on "New" to create a new business rule.
  3. Name your business rule, e.g., "CheckCtaskDatesAgainstCRDates".
  4. Set the "Table" to "Change Task [change_task]" or your corresponding table.
  5. Set the "When to run" to "Before" and "Insert", "Update", and "Delete".
  6. Write a script to check the conditions and display a warning message


    (function executeRule(current, previous /*null when async*/) {
    // Check if the planned start date is prior to CR planned start date
    if (current.planned_start < current.change_request.planned_start) {
    gs.
    addErrorMessage("Warning: Ctask start date is prior to CR planned start date.");
    }
    // Check if the planned end date is after CR planned end date
    if (current.planned_end > current.change_request.planned_end) {
    gs.
    addErrorMessage("Warning: Ctask end date is after CR planned end date. Time extension approval will be required from Change Management.");
    }

    })(current, previous);

    2. Create a Client Script:

    1. Navigate to "System Definition" -> "Client Scripts" in the application navigator.
    2. Click on "New" to create a new client script.
    3. Name your client script, e.g., "CheckCtaskDatesClientScript".
    4. Set the "Table" to "Change Task [change_task]" or your corresponding table.
    5. Set the "Type" to "OnChange" for both planned start and planned end fields.
    6. Write a script to check the conditions and display a warning pop-up.

      function onChange(control, oldValue, newValue, isLoading, isTemplate) {
      // Check if the planned start date is prior to CR planned start date
      if (newValue < g_form.getValue('change_request.planned_start')) {
      alert("Warning: Ctask start date is prior to CR planned start date.");
      g_form.setValue('planned_start', oldValue); // Revert to the old value
      }

      // Check if the planned end date is after CR planned end date
      if (newValue > g_form.getValue('change_request.planned_end')) {
      alert("Warning: Ctask end date is after CR planned end date. Time extension approval will be required from Change Management.");
      g_form.setValue('planned_end', oldValue); // Revert to the old value
      }
      }


      I hope this will help you to get your requirement.

      Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
      Thanks