1

I am trying to write a custom web-api in Appian that will [among other things] return a count of the number of rows in a database table. To that end, I have added this local variable in my api code.

local!countOfRows: a!queryEntity(
    entity: cons!MY_DATABASE_TABLE_DS,
    query: a!query(
      pagingInfo: a!pagingInfo(
        startIndex: 1,
        batchSize: -1
      )
    ),
    fetchTotalCount: true
  ).totalCount,

The idea is that I will then include this value as one of the output's in the json. For example:

local!dataBaseCasesWithDocs: {
    numRecs: local!countOfRows,
    recList: local!listOfRecords
}

So far the recList item works just fine - producing a nice json list of rows of data from my table [albeit 10 at a time]. But when i add the code for the countOfRows using the numRecs field, the function fails with an error 500.

Any thoughts?

[Added extra detail]

I have [also] tried writing a seperate api which [only] returns the row-count for my entity, but it [also] returns error 500...

a!localVariables(
  local!entities: a!queryEntity(
    entity: cons!MY_DATABASE_TABLE_DS,
    query: a!query(
      pagingInfo: a!pagingInfo(
        startIndex: 1,
        batchSize: -1
      )
    ),
    fetchTotalCount: true
  ).totalCount,
  a!httpResponse(
        headers: {
      a!httpHeader(name: "Content-Type", value: "application/json")
    },
    body: a!toJson(value: local!entities)
  )
)

thanks heaps,

David.

4

1 回答 1

1
with(
  local!entities: a!queryEntity(
    entity: cons!MY_DATABASE_TABLE_DS,
    query: a!query(
      pagingInfo: a!pagingInfo(
        startIndex: 1,
        batchSize: 0
      )
    ),
    fetchTotalCount: true
  ).totalCount,
  a!httpResponse(
    headers: {
      a!httpHeader(name: "Content-Type", value: "application/json")
    },
    body: a!toJson({count: local!entities})
  )
)

唯一的区别是我添加了 10 的批量大小。它 [尽管如此] 返回数据库中正确的行数......

通过类似地将批处理大小更改为较小的数字[而不是 -1 来检索所有记录],我使 [原始] 代码也可以正常工作。事实证明,检索所有记录对于获取此 totalCount 字段的正确值不是必需的:

local!countOfRows: a!queryEntity(
    entity: cons!MY_DATABASE_TABLE_DS,
    query: a!query(
      pagingInfo: a!pagingInfo(
        startIndex: 1,
        batchSize: 0
      )
    ),
    fetchTotalCount: true
  ).totalCount,   

实际上,将 batchsize 设置为 0 [for this application] 是最好的选择,因为这会获取元数据(包括 totalCount),而不会实际浪费任何处理时间来检索数据行(在此实例中未使用) -从而提高性能(感谢 Mike Schmitt 的提示)。

于 2020-01-29T07:35:41.287 回答