error when trying to display 1 iteration of a querry thats its empty

a!queryEntity(
  entity: cons!AS_DataStore_Ratings_Pointer,
  query: a!query(
    aggregation: a!queryAggregation(
      aggregationColumns: {
        a!queryAggregationColumn(
          field: "productId",
          isGrouping: true
        ),
        a!queryAggregationColumn(
          field: "rating",
          alias: "rating_average",
          aggregationFunction: "AVG"
        )
      }
    ),
    logicalExpression: a!queryLogicalExpression(
      operator: "AND",
      filters: {
        a!queryFilter(
          field: "productId",
          operator: "=",
          value: ri!productId
        )
      },
      ignoreFiltersWithEmptyValues: true
    ),
    pagingInfo: a!pagingInfo(
      startIndex: 1,
      batchSize: 50
    )
  ),
  fetchTotalCount: false
).data

Hi!

so i have this query which return the average of a value from an ID, but not all items  have rating and when i. use it in a foreach i get this error:

Interface Definition: Expression evaluation error at function a!forEach [line 63]: Error in a!forEach() expression during iteration 3: Expression evaluation error: Invalid index: Cannot index property 'average_rating' of type String into type List of Variant

it says during iteration 3 because its the first time there is no rating on a specific item. Any help?

  Discussion posts and replies are publicly visible

  • +1
    Certified Lead Developer
    in reply to pedrob0002

    Quick draft. Did not test. Should return a valid integer in all cases.

    tointeger(
    index(
    index(
    a!queryEntity(
      entity: cons!AS_DataStore_Ratings_Pointer,
      query: a!query(
        aggregation: a!queryAggregation(
          aggregationColumns: {
            a!queryAggregationColumn(
              field: "productId",
              isGrouping: true
            ),
            a!queryAggregationColumn(
              field: "rating",
              alias: "rating_average",
              aggregationFunction: "AVG"
            )
          }
        ),
        logicalExpression: a!queryLogicalExpression(
          operator: "AND",
          filters: {
            a!queryFilter(
              field: "productId",
              operator: "=",
              value: ri!productId
            )
          },
          ignoreFiltersWithEmptyValues: true
        ),
        pagingInfo: a!pagingInfo(
          startIndex: 1,
          batchSize: 50
        )
      ),
      fetchTotalCount: false
    ).data,
    1,
    null
    ),
    rating_average,
    0
    )
    )

  • will test it out, is it with the two index functions? looks weird 

  • it works thank you so much,

    but could you explain the meaning of 2 indexes? just so I understand

  • 0
    Certified Lead Developer
    in reply to pedrob0002

    The inner index picks the first item in the list of returned rows. The outer index picks the value of the field and in case the is nothing a zero.

    All my expressions returning data, always return the same data type. Even if it is a casted null value. This makes it so much easier to work with them.

  • +1
    Certified Lead Developer
    in reply to Stefan Helzle

    Shouldn't line 41 be in quotes, i.e. "rating_average", to provide the property string?  I don't know what it would do if not encapsulated like that, I would assume it would give an error. 

    Also IMHO this should be a property() call instead of nested index(); this scenario is a clear example of why these functions should be used for their "namely" purposes, even if they're identical on the back-end.

    tointeger(
      property(
        index(
          a!queryEntity(
            entity: cons!AS_DataStore_Ratings_Pointer,
            query: a!query(
              aggregation: a!queryAggregation(
                aggregationColumns: {
                  a!queryAggregationColumn(
                    field: "productId",
                    isGrouping: true
                  ),
                  a!queryAggregationColumn(
                    field: "rating",
                    alias: "rating_average",
                    aggregationFunction: "AVG"
                  )
                }
              ),
              logicalExpression: a!queryLogicalExpression(
                operator: "AND",
                filters: {
                  a!queryFilter(
                    field: "productId",
                    operator: "=",
                    value: ri!productId
                  )
                },
                ignoreFiltersWithEmptyValues: true
              ),
              pagingInfo: a!pagingInfo(
                startIndex: 1,
                batchSize: 50
              )
            ),
            fetchTotalCount: false
          ).data,
          1,
          null
        ),
        "rating_average",
        0
      )
    )