Skip to main content
29/31

Sort, Page and Total a Grid Without a Query

Medium

A read-only grid shows invoices that are already loaded into a local variable: maps with id, customer and amount. The grid sorts and pages them itself, and a footer shows totals.


Given the rows, a sort field, a direction, a start index and a batch size, return one map:

  • data: the rows of the requested page, after sorting all rows
  • totalCount: the number of rows in all pages
  • pageTotal: the sum of amount on this page
  • grandTotal: the sum of amount across all rows
  • hasNext: true when there is a page after this one

Every amount is a Decimal, written 80.0 rather than 80. In Appian, todatasubset() will not sort on a field that mixes Integer and Decimal values; it stops with "The sort field must be a consistent type".


Why it comes up: a grid built on an integration response or a local list has to sort and page in memory. The footer is where people slip: they sum the current page and show it as the total.

ri! Rule Inputs

ri!rowsList of Map
{
  a!map(id: 1, customer: "Zeta", amount: 120.5),
  a!map(id: 2, customer: "Acme", amount: 80.0),
  a!map(id: 3, customer: "Mono", amount: 300.0),
  a!map(id: 4, customer: "Beta", amount: 80.0),
  a!map(id: 5, customer: "Kilo", amount: 45.0)
}
ri!sortFieldText
"amount"
ri!ascendingBoolean
false
ri!startIndexInteger
1
ri!batchSizeInteger
2

Example 1: Largest amounts first

Input: ri!rows
{
  a!map(id: 1, customer: "Zeta", amount: 120.5),
  a!map(id: 2, customer: "Acme", amount: 80.0),
  a!map(id: 3, customer: "Mono", amount: 300.0),
  a!map(id: 4, customer: "Beta", amount: 80.0),
  a!map(id: 5, customer: "Kilo", amount: 45.0)
}
Input: ri!sortField
"amount"
Input: ri!ascending
false
Input: ri!startIndex
1
Input: ri!batchSize
2
Expected output:
a!map(data: {a!map(id: 3, customer: "Mono", amount: 300.0), a!map(id: 1, customer: "Zeta", amount: 120.5)}, totalCount: 5, pageTotal: 420.5, grandTotal: 625.5, hasNext: true)

Example 2: Second page by customer

Input: ri!rows
{
  a!map(id: 1, customer: "Zeta", amount: 120.5),
  a!map(id: 2, customer: "Acme", amount: 80.0),
  a!map(id: 3, customer: "Mono", amount: 300.0),
  a!map(id: 4, customer: "Beta", amount: 80.0),
  a!map(id: 5, customer: "Kilo", amount: 45.0)
}
Input: ri!sortField
"customer"
Input: ri!ascending
true
Input: ri!startIndex
3
Input: ri!batchSize
2
Expected output:
a!map(data: {a!map(id: 5, customer: "Kilo", amount: 45.0), a!map(id: 3, customer: "Mono", amount: 300.0)}, totalCount: 5, pageTotal: 345, grandTotal: 625.5, hasNext: true)

Example 3: Short last page

Input: ri!rows
{
  a!map(id: 1, customer: "Zeta", amount: 120.5),
  a!map(id: 2, customer: "Acme", amount: 80.0),
  a!map(id: 3, customer: "Mono", amount: 300.0),
  a!map(id: 4, customer: "Beta", amount: 80.0),
  a!map(id: 5, customer: "Kilo", amount: 45.0)
}
Input: ri!sortField
"customer"
Input: ri!ascending
true
Input: ri!startIndex
5
Input: ri!batchSize
2
Expected output:
a!map(data: {a!map(id: 1, customer: "Zeta", amount: 120.5)}, totalCount: 5, pageTotal: 120.5, grandTotal: 625.5, hasNext: false)

Example 4: Everything on one page

Input: ri!rows
{
  a!map(id: 1, customer: "Zeta", amount: 120.5),
  a!map(id: 2, customer: "Acme", amount: 80.0),
  a!map(id: 3, customer: "Mono", amount: 300.0),
  a!map(id: 4, customer: "Beta", amount: 80.0),
  a!map(id: 5, customer: "Kilo", amount: 45.0)
}
Input: ri!sortField
"id"
Input: ri!ascending
false
Input: ri!startIndex
1
Input: ri!batchSize
10
Expected output:
a!map(data: {a!map(id: 5, customer: "Kilo", amount: 45.0), a!map(id: 4, customer: "Beta", amount: 80.0), a!map(id: 3, customer: "Mono", amount: 300.0), a!map(id: 2, customer: "Acme", amount: 80.0), a!map(id: 1, customer: "Zeta", amount: 120.5)}, totalCount: 5, pageTotal: 625.5, grandTotal: 625.5, hasNext: false)

Appian Expression Tips

  • • Use ri!variableName for rule inputs
  • • Use a!localVariables(local!x: …, body) for locals
  • • Use a!forEach(items: …, expression: fv!item) to iterate
  • • Use where() / reject(fn!isnull, list) to filter