Query to search a date in date range is not working

Hi,

I an trying to filter records from DB where selected date is between startDate & endDate in the query below but for some reason I am not getting any result, please help me with it.

Expression Rule:

a!localVariables(
  local!sampleData: a!queryEntity(
    entity: cons!DATA,
    query: a!query(
      logicalexpression: a!queryLogicalExpression(
        operator: "AND",
        filters: {
          a!queryFilter(
            field: "userid",
            operator: "=",
            value: ri!userid
          ),
          a!queryFilter(
            field: "startdatetime",
            operator: ">=",
            value: gmt(todate(ri!selectedDate)),
            applyWhen: not(isnull(ri!selectedDate))
          ),
          a!queryFilter(
            field: "enddatetime",
            operator: "<=",
            value: gmt(todate(ri!selectedDate)),
            applyWhen: not(isnull(ri!selectedDate))
          )
        },
        ignoreFiltersWithEmptyValues: true
      ),
      pagingInfo: a!pagingInfo(
        startIndex: 1,
        batchSize: - 1,
        sort: a!sortInfo(field: "createddate", ascending: true)
      )
    ),
    fetchTotalCount: false
  ).data,
  {
    if(
      not(isnull(local!sampleData)),
      local!sampleData,
      {}
    )
  }
)

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Senior Developer

    Have tried converting the ri!selectedDate todatetime instead of todate? That might be causing issues in the gmt conversion and if the field you're searching is a datetime it appears. Another thing is you don't need the not(isnull(ri!selectedDate)) since you have the ignoreFiltersWithEmptyValues equal to true

  • I have modified filter operators and now the result looks correct, Except one issue.

    If START_DATE is 02/23/2021END_DATE is 02/26/20021, and I am checking if the date 02/24/2021 exist in between STATRT_DATE & END_DATE the result is true and its true for 02/25/2021 & 02/26/2021 as well but if I am looking for 02/23/2021 it gives me false. Please have a look what's wrong with it, I have already checked it with todate() & todatetime() functions but no luck.

    Expression Rule

    a!queryEntity(
      entity: cons!DATA,
      query: a!query(
        logicalexpression: a!queryLogicalExpression(
          operator: "AND",
          filters: a!queryFilter(
            field: "userid",
            operator: "=",
            value: ri!userid
          ),
          logicalExpressions: a!queryLogicalExpression(
            operator: "AND",
            filters: {
              a!queryFilter(
                field: "startdatetime",
                operator: "<=",
                value: gmt(todate(ri!selecteddate)),
                applyWhen: not(isnull(ri!selecteddate))
              ),
              a!queryFilter(
                field: "enddatetime",
                operator: ">=",
                value: gmt(todate(ri!selecteddate)),
                applyWhen: not(isnull(ri!selecteddate))
              )
            }
          ),
          ignoreFiltersWithEmptyValues: true
        ),
        pagingInfo: a!pagingInfo(
          startIndex: 1,
          batchSize: - 1,
          sort: a!sortInfo(field: "createddate", ascending: true)
        )
      ),
      fetchTotalCount: false
    ).data

  • 0
    Certified Lead Developer
    in reply to shamima0001

    May I ask why you use gmt()?

  • May I ask why you use gmt()?

    I guess dates are stored in GMT in DB and to compare dates from DB parameter dates should also be converted to GMT. Isn't it. 

  • 0
    Certified Lead Developer
    in reply to shamima0001

    By default Appian stores all date and time values as GMT in DB. But it also translates this into the users time zone automatically. This means, that if you add another layer of translation to this, you might end up turning 02/23/2021 into 02/22/2021.

    I strongly suggest to not do any timezone translations unless there is a very specific need.

Reply Children