Skip to main content
27/31

Upsert Changes into a List of Rows

Medium

An editable grid holds ri!rows, a list of maps with id, name and qty. An integration returns ri!changes in the same shape. Each id appears at most once in each list.


Return the rows after applying the changes:

  • A change whose id already exists replaces that row, in the row's current position.
  • A change with a new id is added at the end, in the order the changes arrive.
  • Rows with no change stay exactly as they are.

Why it comes up: you write this merge after an integration refresh and before Write Records. Merging in place keeps the rows in the order the user was already looking at.

ri! Rule Inputs

ri!rowsList of Map
{
  a!map(id: 1, name: "Bolt", qty: 10),
  a!map(id: 2, name: "Nut", qty: 5),
  a!map(id: 3, name: "Washer", qty: 0)
}
ri!changesList of Map
{
  a!map(id: 2, name: "Nut", qty: 8),
  a!map(id: 4, name: "Screw", qty: 20)
}

Example 1: One update, one insert

Input: ri!rows
{
  a!map(id: 1, name: "Bolt", qty: 10),
  a!map(id: 2, name: "Nut", qty: 5),
  a!map(id: 3, name: "Washer", qty: 0)
}
Input: ri!changes
{
  a!map(id: 2, name: "Nut", qty: 8),
  a!map(id: 4, name: "Screw", qty: 20)
}
Expected output:
{
  a!map(id: 1, name: "Bolt", qty: 10),
  a!map(id: 2, name: "Nut", qty: 8),
  a!map(id: 3, name: "Washer", qty: 0),
  a!map(id: 4, name: "Screw", qty: 20)
}

Example 2: No changes

Input: ri!rows
{
  a!map(id: 1, name: "A", qty: 1),
  a!map(id: 2, name: "B", qty: 2)
}
Input: ri!changes
{}
Expected output:
{
  a!map(id: 1, name: "A", qty: 1),
  a!map(id: 2, name: "B", qty: 2)
}

Example 3: Empty grid

Input: ri!rows
{}
Input: ri!changes
{
  a!map(id: 5, name: "X", qty: 1),
  a!map(id: 6, name: "Y", qty: 2)
}
Expected output:
{
  a!map(id: 5, name: "X", qty: 1),
  a!map(id: 6, name: "Y", qty: 2)
}

Example 4: Updates arrive out of order

Input: ri!rows
{
  a!map(id: 1, name: "A", qty: 1),
  a!map(id: 2, name: "B", qty: 2),
  a!map(id: 3, name: "C", qty: 3)
}
Input: ri!changes
{
  a!map(id: 3, name: "C2", qty: 30),
  a!map(id: 1, name: "A2", qty: 10)
}
Expected output:
{
  a!map(id: 1, name: "A2", qty: 10),
  a!map(id: 2, name: "B", qty: 2),
  a!map(id: 3, name: "C2", qty: 30)
}

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