# Important Lot Functions

| Function | What it does |
| --- | --- |
| `system.kanoa.lot.executeLotOperation` | Creates a lot operation event with all its input and output lot events in one call, so the genealogy is never left half-written |
| `system.kanoa.lot.getLotActivity` | Returns the lots that currently have material, with quantity by asset and state, filtered by asset, lot name, transport and more |

A transfer screen uses both in a loop:

1. The user picks a source asset, and `getLotActivity` lists the lots there.
2. The user picks a lot, a quantity and a destination.
3. `executeLotOperation` runs a `Transfer`: an input at the source and an output at the destination.
4. The screen calls `getLotActivity` again to refresh.

A receiving screen only needs the second function: an operation with no inputs and one output.

## Build a Whole Trace from a Script

The script below runs every lot operation from [Execute the Trace](03-execute-the-trace.md) with the lot functions: it receives the aluminum and coating ingredients, machines, blends, coats, inspects, packs and ships, then prints the package's trace. Every operation follows the same pattern:

1. **The operation**: which lot operation, at which asset, at what time.
2. **Its inputs**: existing lots, how much is taken, and the asset they're taken from.
3. **Its outputs**: the same lot at a new asset, as a transfer does, or a new lot with a new name, item and unit.
4. **One call** to `executeLotOperation` with all of them, so the operation and its lot events are written together.

| Function | How the script uses it |
| --- | --- |
| `system.kanoa.serial.getNextSerialNumber` | Names each new lot from its serialization, such as `RAL0000000001` |
| `system.kanoa.lot.executeLotOperation` | Records each operation. An output lot with no `lotId` is created from its name, item and unit |
| `system.kanoa.lot.getLots` | Finds the new lots' ids by name, for the operations that use them next |
| `system.kanoa.lot.traceLot` | Returns every lot event linked to the package, backward and forward |
| `system.kanoa.lot.getLotActivity` | Shows the material left in inventory |

:::caution[It writes lot data]
Run it only on a training gateway that has finished [Lot Configuration](02-lot-configuration.md). Every run receives new material and makes a new set of lots.
:::

:::action[Run the trace from a script]

1. Copy the script into the Ignition Designer Script Console (**Tools → Script Console**) and run it. It prints each operation, the package's trace, and the material left.
2. In **Ops → Lots → Lot Manager**, find the new package lot (`FGP000000000x`), clear **Active** and click **Get lots** to show lots with no material, and compare its **Trace** tab with the trace the script printed. Set the depth button to `D`: the script traces to the same depth.

<details>
<summary>Show the script</summary>

::check-script{src="trace/_execute-the-trace-example.py" title="executeTheTrace.py"}

</details>

:::

:::tip[Building your own screen]
Run the operation by hand in Lot Manager first. Note its inputs and outputs, build the same payload in your script, check quantities before you call `executeLotOperation`, and show a clear success or error message.
:::

::embed-section{src="../../systemFunctions/lot/executeLotOperation.md" collapsed="true" summary="Reference Materials: executeLotOperation"}

::embed-section{src="../../systemFunctions/lot/getLotActivity.md" collapsed="true" summary="Reference Materials: getLotActivity"}
