Overlapping Range Check

Certified Senior Developer

Hi,

There are two date fields in database 'startDate' and 'endDate' for every record. I want to check overlapping conditions while user try to insert new set of date range for a new record. For eg. if we had 'startDate' and 'endDate' as '20 Dec 2019' and '5 Jan 2020' respectively, we cannot add new 'startDate' and 'endDate' as '25 Dec 2019' and '15 Jan 2020' because it is overlapping.

  Discussion posts and replies are publicly visible

  • I think the easiest way to do this is to query the database and use a logical expression to check for 2 possible conditions:

    1. An existing range starts before the start date you provided and ends after the start date you provided
    2. An existing range starts before the end date you provided and ends after the end date you provided

    If either of those conditions are true, then the ranges overlap. Here's a sample code to perform the query:

    a!queryEntity(
      entity: cons!<YOUR ENTITY HERE>,
      query: a!query(
        pagingInfo: a!pagingInfo(
          startIndex: 1,
          batchSize: -1
        ),
        logicalExpression: a!queryLogicalExpression(
          operator: "OR",
          logicalExpressions: {
            a!queryLogicalExpression(
              operator: "AND",
              filters: {
                a!queryFilter(
                  field: "startDate",
                  operator: "<",
                  value: ri!newStartDate
                ),
                a!queryFilter(
                  field: "endDate",
                  operator: ">",
                  value: ri!newStartDate
                )
              }
            ),
            a!queryLogicalExpression(
              operator: "AND",
              filters: {
                a!queryFilter(
                  field: "startDate",
                  operator: "<",
                  value: ri!newEndDate
                ),
                a!queryFilter(
                  field: "endDate",
                  operator: ">",
                  value: ri!newEndDate
                )
              }
            )
          }
        )
      ),
      fetchTotalCount: false
    )

  • I believe there is one additional scenario to consider, when an existing range starts after the start date and ends prior to the end date (provided dates surrounding completely).  All can be covered by reducing to one scenario:

    1. An existing range starts <= to the end date you provided and ends >= the start date you provided

    a!queryEntity(
      entity: cons!<YOUR ENTITY HERE>,
      query: a!query(
        pagingInfo: a!pagingInfo(
          startIndex: 1,
          batchSize: -1
        ),
        logicalExpression: a!queryLogicalExpression(
          operator: "AND",
          filters: {
            a!queryFilter(
              field: "startDate",
              operator: "<=",
              value: ri!newEndDate
            ),
            a!queryFilter(
              field: "endDate",
              operator: ">=",
              value: ri!newStartDate
            )
          }
        )
      ),
      fetchTotalCount: false
    )

  • 0
    Appian Employee
    in reply to Chris

    Ah good call - you're right I missed one Slight smile