5/5

a!forEach(): print the lines of an invoice

Hard

An invoice lists the order lines being billed, numbered from 1, followed by a total. Each order line is a record with name, qty and price. A line with a quantity of 0 was cancelled.

Return a list of text lines

  • one line per billed item, numbered: "1. Mug x2 = 10", where 10 is quantity × price
  • a last line with the total of the billed items: "Total: 25"

A cancelled line is left off the invoice entirely.

Watch out

  • The numbers count the printed lines, so a cancelled line leaves no gap: "1. Mug" then "2. Lamp", even when a cancelled pen sat between them.
  • fv!index counts every item of the list being looped over, cancelled ones included.

Input

  • ri!lines — List of Map, each with name (Text), qty (Integer) and price (Integer)

ri! Rule Inputs

ri!linesList of Map
{
  a!map(name: "Mug", qty: 2, price: 5),
  a!map(name: "Pen", qty: 0, price: 1),
  a!map(name: "Lamp", qty: 1, price: 15)
}

Example 1 — A cancelled line in the middle

Input: ri!lines
{
  a!map(name: "Mug", qty: 2, price: 5),
  a!map(name: "Pen", qty: 0, price: 1),
  a!map(name: "Lamp", qty: 1, price: 15)
}
Output:
{
  "1. Mug x2 = 10",
  "2. Lamp x1 = 15",
  "Total: 25"
}

Example 2 — A single line

Input: ri!lines
{a!map(name: "Rug", qty: 3, price: 20)}
Output:
{
  "1. Rug x3 = 60",
  "Total: 60"
}

Example 3 — A cancelled first line

Input: ri!lines
{
  a!map(name: "Pen", qty: 0, price: 1),
  a!map(name: "Cup", qty: 4, price: 3)
}
Output:
{
  "1. Cup x4 = 12",
  "Total: 12"
}

Example 4 — Everything cancelled

Input: ri!lines
{a!map(name: "Pen", qty: 0, price: 1)}
Output:
{"Total: 0"}

Example 5 — An empty order

Input: ri!lines
{}
Output:
{"Total: 0"}

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