# Workflows

Audience: Logistics Manager

How automated workflows control and customize warehouse operations

## What are workflows?

Workflows in Pyck are automated processes that help customize and control warehouse operations. Each workflow is a series of predefined steps that executes automatically when certain events happen in your warehouse.

## How workflows work

1. **Event Triggers**: Workflows start when specific events occur in your warehouse system. For example:
   - A new order arrives
   - Inventory falls below a threshold
   - A shipment is received
   - A picking task is completed

2. **Automated Actions**: Once triggered, workflows follow a predefined sequence of activities without requiring manual intervention.

3. **Customization**: Each warehouse can have its own set of workflows tailored to its specific processes and requirements.

## Benefits of using workflows

Workflows make warehouse processes follow the same steps every time, and they automate repetitive tasks to save time and reduce errors. You can modify your warehouse processes without changing the core system, and you can track and monitor process execution across your facility. Workflows continue running even if there are system failures, and tasks are automatically retried if they fail, so they complete.

## Common workflow examples

- **Receiving Process**: Automatically update inventory when goods arrive and notify relevant departments
- **Order Fulfillment**: Guide picking, packing, and shipping activities in the most efficient sequence
- **Inventory Management**: Trigger reordering when stock levels drop below defined thresholds
- **Quality Control**: Route items through inspection processes based on predefined rules

## Dynamic app behavior

Workflows can change the behavior of the mobile apps and devices used in your warehouse. A workflow can modify what screens and options appear on warehouse devices, and workers receive different instructions based on the specific circumstances. Apps can adapt immediately to changing conditions in the warehouse, and you can create unique step-by-step guides for different products, customers, or situations.

For example, a picking workflow could change a mobile app to show different picking instructions based on the item type, storage location, or even the experience level of the warehouse worker. For how a workflow controls the screens of the app, see [Frontend control points](https://docs.pyck.cloud/latest/developers/workflows.md#frontend-control-points).

## How to implement workflows

Pyck uses Business Process Model and Notation (BPMN) as a common language between logistics managers and technical teams. You create flowcharts that show the steps in your warehouse operations, share your process requirements with technical teams in standard notation, and review that the technical implementation matches your operational needs.

AI coding agents generate the workflow code from your BPMN models, on your own isolated codebase. Developers review and quality-assure the generated code instead of writing it by hand, so a change to a process lands in hours or days. The code runs on Temporal.io, a workflow engine that keeps workflows reliable and durable, and is written primarily in Go using the Pyck Go SDK.

## Triggering a workflow

Pyck has no on/off switch on a workflow definition. A workflow runs when a worker has registered a subscription, called a workflow signal, for it. Each subscription names an event topic, a signal type and an optional filter rule. A worker keeps its subscriptions alive with a heartbeat, and a subscription expires one hour after the last heartbeat. To stop a workflow from being triggered, stop its workers and remove the workflow with `deleteWorkflow`.


## Filtering events with `FilterRule`

### What is the `FilterRule` ?
- **Purpose:** A conditional expression that decides whether an incoming event should trigger the workflow. It belongs to one subscription (a workflow signal, field `filterRule`), not to the workflow definition.
- **Syntax:** Uses FEEL (Friendly Enough Expression Language), which supports logical operators, comparisons, and built-in functions.

### How to write a rule
1. Reference event fields by name: if your event data includes a field `name`, use `name` in the expression.
2. Built-in functions: such as `starts with(text, prefix)`, `contains(text, substring)`, numeric comparisons (such as `>`, `<` and `=`).
3. Logical operators: combine conditions with `and`, `or`, `not`.

### Example: Trigger only when item names start with "item"
- **Workflow name:** `TemporalWorkflowPrintItem_ItemCreate`
- **Filter rule:** `starts with(name, "item")`

#### What happens at runtime:
1. A client runs the `createInventoryItem` mutation
2. The system emits an event on the topic `pyck.<tenant>.crud.inventory.item.<item ID>.create` with this `eventData`. This is a CRUD event topic, `pyck.<tenant>.crud.<service>.<schema>.<entity>.<operation>`, with the service `inventory` and the item's ID as the entity (see [Event namespaces](https://docs.pyck.cloud/latest/developers/workflows.md#event-namespaces)).
3. Workflow-service checks every subscription whose topic matches this event.
4. For `TemporalWorkflowPrintItem_ItemCreate`, it evaluates `starts with(name, "item")`:
   - If `name = "item_123"`, expression is **true** -> the workflow is launched.
   - If `name = "product_456"`, expression is **false** -> the workflow is skipped.
