Comparing Dates ‐ Scoped vs. Global Applications - ben-vargas/servicenow-wiki GitHub Wiki

Comparing date and time fields is a common requirement in ServiceNow development. Whether you are validating form inputs, enforcing business logic, or preventing invalid timelines, understanding how to compare dates accurately is critical.

The methods available to you can differ depending on whether you are working in a scoped or global application. Scoped applications have additional restrictions that limit access to certain global APIs, while global applications allow use of more traditional methods.

This guide outlines approaches for comparing dates in both contexts and provides best practices and references to help you choose the optimal method.


Date Comparison in Scoped Applications

In scoped applications, certain global APIs like getGlideObject() are not available. Instead, rely on the GlideDateTime class and its built-in comparison methods. This approach is straightforward, readable, and keeps your code self-contained.

Example: Ensuring a Due Date is in the Future

if (!current.due_date.nil()) {
    var dueDateTime = new GlideDateTime(current.due_date);
    var nowDateTime = new GlideDateTime();

    // Check if dueDateTime occurs before nowDateTime
    if (dueDateTime.before(nowDateTime)) {
        gs.addInfoMessage('Completion must be after the current time');
        current.due_date.setError('Completion must be after the current time');
        current.setAbortAction(true);
    }
}

Key Points:

  • Initialization: Create GlideDateTime objects for both the field (e.g., due_date) and the current system time.
  • Comparison: Use before(), after(), or compareTo() methods provided by GlideDateTime. In this example, before() checks if the due date is earlier than the current time.
  • Error Handling: If the due date is invalid, provide user feedback via gs.addInfoMessage() and field-level errors using setError().

Additional Methods:

  • after(): Verify if one date is after another.
  • compareTo(): Returns a numeric value (0, negative, or positive) indicating whether one date is equal, before, or after another.

Date Comparison in Global Applications

In global applications, you have access to methods like getGlideObject() and getNumericValue() that are restricted in scoped apps. These methods allow you to convert dates into numeric timestamps, making them easy to compare as numbers.

Example: Validating Start and End Dates

if (!current.u_date1.nil() && !current.u_date2.nil()) {
    var start = current.u_date1.getGlideObject().getNumericValue();
    var end = current.u_date2.getGlideObject().getNumericValue();

    if (start > end) {
        gs.addInfoMessage('Start date must be before end date');
        current.u_date1.setError('Start date must be before end date');
        current.setAbortAction(true);
    }
}

Key Points:

  • Numeric Conversion: getNumericValue() returns the date as the number of milliseconds since the epoch, enabling simple numeric comparisons.
  • Direct Comparison: Check if start > end to ensure chronological correctness.
  • Error Handling: Similar to the scoped example, add messages and errors to inform the user of invalid input.

Key Differences and Best Practices

  1. Scoped vs. Global Methods:

    • Scoped Apps: Use GlideDateTime methods (before(), after(), compareTo()).
    • Global Apps: Can use getGlideObject() and getNumericValue() for direct numeric comparisons.
  2. Maintainability and Clarity:

    • Relying on GlideDateTime methods often makes code more readable and self-explanatory.
    • Numeric comparisons are straightforward but can be less intuitive without comments or descriptive variable names.
  3. Consistency:

    • Choose a consistent approach for your application type and stick to it for clarity and maintainability.
    • In mixed environments, document which approach is used and why.

Additional References


Conclusion

Date comparison in ServiceNow depends on your application’s scope. In scoped applications, use GlideDateTime methods for a clean, built-in solution. In global applications, take advantage of getGlideObject() and getNumericValue() for numeric comparisons.

By selecting the right method for your context and following best practices, you ensure accurate and maintainable date validations that enhance the user experience and data integrity within your instance.