4/5

a!match(): days of stock left without dividing by zero

Medium

A warehouse screen shows how long each product's stock will last, from the units on the shelf and the units sold per day.

Return

  • "Out of stock" — nothing on the shelf
  • "No recent sales" — stock, but no sales at all
  • "Less than a day left" — less stock than one day's sales
  • "Days left: 12" — otherwise, the whole days the stock lasts

Watch out

  • An empty shelf is out of stock even when nothing is selling.
  • Whole days means the integer part of stock ÷ daily sales, which is what quotient(numerator, denominator) returns.
  • With no sales the division is by zero, which is an error. default is only reached when no condition held, so a division there never sees a zero an earlier condition caught.

Inputs

  • ri!stock — Integer
  • ri!dailySales — Integer

ri! Rule Inputs

ri!stockInteger
120
ri!dailySalesInteger
10

Example 1 — Twelve days of stock

Input: ri!stock
120
Input: ri!dailySales
10
Output:
"Days left: 12"

Example 2 — Part days do not count

Input: ri!stock
125
Input: ri!dailySales
10
Output:
"Days left: 12"

Example 3 — Exactly one day

Input: ri!stock
15
Input: ri!dailySales
10
Output:
"Days left: 1"

Example 4 — Will not last the day

Input: ri!stock
5
Input: ri!dailySales
10
Output:
"Less than a day left"

Example 5 — Empty shelf

Input: ri!stock
0
Input: ri!dailySales
10
Output:
"Out of stock"

Example 6 — Nothing selling

Input: ri!stock
40
Input: ri!dailySales
0
Output:
"No recent sales"

Example 7 — Empty shelf, nothing selling

Input: ri!stock
0
Input: ri!dailySales
0
Output:
"Out of stock"

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