Skip to content

Workflow Rules

Workflow rules automate actions in response to CRM entity events. When entities or deals are updated, active rules are evaluated and matching actions are executed.

WorkflowRule Model

Field Type Default Description
name str required Rule name
entity_type Literal['Entity', 'Deal', 'Activity'] required Which model this rule applies to
trigger_event Literal['create', 'update', 'delete'] required Event that triggers the rule
condition_field str \| None None Field to evaluate (no condition = always match)
condition_operator See below None Comparison operator
condition_value Any \| None None Value to compare against
action_type Literal['update_field', 'create_activity', 'send_notification'] required Action to execute
action_config dict[str, Any] required Action configuration (structure depends on action type)
is_active bool True Whether the rule is active

Condition Operators

Operator Description
equals Field value equals the condition value
not_equals Field value does not equal the condition value
contains Field value (as string) contains the condition value (as string)
greater_than Field value is greater than the condition value
less_than Field value is less than the condition value

If condition_field is None, the rule always matches (no condition check).

Action Types

update_field

Updates a field on the triggering entity.

action_config:

{
    "field_name": "status",
    "value": "Active"
}

The action calls setattr(entity, field_name, value) followed by entity.save().

create_activity

Creates a Note activity linked to the triggering entity.

action_config:

{
    "subject": "Follow-up required",
    "description": "Automatically created by workflow rule"
}

The note is linked to the entity via related_to_type / related_to_id and inherits the entity's assigned_to if available.

send_notification

Placeholder for future notification integration. Currently a no-op.

How Rules Are Triggered

Workflow rules are executed from post_update hooks on Entity and Deal models:

Entity.post_update() → WorkflowService.execute_rules('Entity', 'update', entity)
Deal.post_update()   → WorkflowService.execute_rules('Deal', 'update', deal)

The WorkflowService loads all active rules matching the entity type and trigger event, evaluates conditions, and executes actions. If an action fails, a WorkflowExecutionError is raised.

Example

from amsdal_crm.models.workflow_rule import WorkflowRule

# Auto-create a follow-up note when a deal status changes to closed_won
WorkflowRule(
    name='Won Deal Follow-up',
    entity_type='Deal',
    trigger_event='update',
    condition_field='status',
    condition_operator='equals',
    condition_value='closed_won',
    action_type='create_activity',
    action_config={
        'subject': 'Deal won — schedule onboarding',
        'description': 'This deal has been closed. Please schedule an onboarding call with the client.',
    },
    is_active=True,
).save(force_insert=True)

# Auto-update entity status when name contains "VIP"
WorkflowRule(
    name='VIP Entity Marker',
    entity_type='Entity',
    trigger_event='update',
    condition_field='name',
    condition_operator='contains',
    condition_value='VIP',
    action_type='update_field',
    action_config={
        'field_name': 'note',
        'value': 'VIP client — priority support',
    },
    is_active=True,
).save(force_insert=True)

Configuration

The maximum number of workflow rules per entity type is controlled by:

AMSDAL_CRM_MAX_WORKFLOW_RULES_PER_ENTITY=100  # default