Chain two rules with a custom event
Make one rule start another: define a custom event type, let the second rule listen to it with the On Event trigger, and have the first rule raise it when it finishes.
Chain two rules with a custom event
Rules never set each other off on their own: whatever one rule changes is ignored by the others, so rules can't loop. When you do want one rule to follow another, for example "when a demand goes 30 days overdue, escalate it; when it is escalated, give the Sales Manager a task", you connect them with a custom event. The first rule raises the event when it finishes; the second listens to it.
Before you start
- You need the Create permission on Workflow Rules to add an event type and the Edit permission on Workflow Rules to change the rules. In the default roles only the Administrator has them.
- Both rules must watch the same entity (both on Demand, say), and the event type must be of that entity too. The app refuses a mix.
- Both rules must be your own (custom) rules. System rules can't be changed; clone one first (see Customise a system rule). To build a rule from scratch, see Build and switch on a custom rule.
Steps
-
Open the account menu > Settings > Workflows > Event types (Event types). The page lists the system events, marked with a lock icon, and any custom ones.
-
Click New event type. The panel "New event type" opens, pre-filled with the entity you have filtered on and sort order 100.
-
Fill in the panel and click Add event type:
- Code (required): capital letters, digits and underscores, starting with a letter and unique in your organization, for example DEMAND_ESCALATED (hint "A–Z, digits, _ ; unique in your organization");
- Name (required), for example "Demand escalated";
- Entity (required): the kind of record both rules watch;
- Description: "Optional; shown to people picking the event for a rule.";
- Sort order and Active (keep it ticked).
You see '"<name>" added.' The new event appears without a lock icon, with Rules 0.
-
Open the rule that should run second (Rule). In Trigger, choose On Event and pick your event under Event type (entries read "name (code)"; placeholder "Choose an event type…"). Make sure the rule is Active, then click Save changes. You see '"<name>" saved as version <n>.' and the trigger badge reads On Event.
-
Open the rule that should run first. In the Advanced section ("Chain rules by raising a custom event when this one finishes."), click Show and pick your event under Emits event type. Click Save changes. You see '"<name>" saved as version <n>.'; with the section collapsed it reads "Raises a custom event after the actions run."
-
Let the first rule run on a record (or wait for its trigger). Then open the second rule and click Run history (Runs). Open its latest run with the eye icon: Trigger event shows your code, Trace depth reads 1, and Outbox event links to the event the first rule raised (Outbox; the grid marks it "depth 1" and its panel shows Trace depth 1 and, when the first rule was itself started by an event, a Parent event).
What happens next
- The event is raised only when the first rule succeeds. If the first rule's run ends Errored, the second rule doesn't run. A step that failed with Continue on error ticked still counts as success.
- The event is raised only while someone listens. If no active rule listens to the event when the first rule finishes (the second rule is switched off, or scoped to another project), nothing is raised and nothing is queued for later.
- Chains stop two levels deep. The second rule may raise another custom event for a third rule, but a rule raised beyond that never runs: each listener gets a Blocked run with "Blocked: <CODE> from rule "<name>" would run at trace depth <n> (maximum <m>)". This stops runaway loops.
- The event type now counts its listeners in the Rules column, and it can't be deleted while rules use it.
- To check the second rule without waiting, use Test on it and add the values the event would carry under Event extras.
If something goes wrong
- "The event type belongs to a different entity than the rule." when saving the first rule: the Emits event type list offers custom events of every entity, but the rule may only raise one of its own entity. "A rule can only emit a custom event type" and "A rule cannot emit the event it listens to" have the same fix: pick another custom event of the rule's entity. "Choose the event type this rule listens to" on the second rule: pick the event. The event isn't offered under Event type: it belongs to another entity, or it is inactive. See Rule cannot be saved (trouble.workflows.rule-cannot-be-saved).
- "An event type with this code already exists." or "This code is reserved for the engine's own event types." when you click Add event type: choose another code. See Already exists (trouble.admin.already-exists).
- The second rule never runs: check that the first rule's run ended Success, that the second rule is Active and in scope for the record's project, and that the event type is still Active (making a custom event inactive also stops the first rule raising it). See Investigate a failed automation.
- A run reads Blocked with "Blocked: … would run at trace depth …": the chain is deeper than two levels. Shorten it. See Action failed (trouble.workflows.action-failed).
- "Rules listen to this event type. Deactivate it or change those rules first." when deleting the event type or changing its entity: rules still listen to it or raise it, or recent outbox events refer to it. See Rule cannot be deleted (trouble.workflows.rule-cannot-be-deleted).
Related
Related
- Event typesScreen
The named events a rule can listen to: the system events NirmaanOne raises itself and the custom events you add to chain rules or to raise from your own integrations.
- RuleScreen
One automation rule's page: read or change its basics, trigger, conditions and actions, test it against a record, clone or delete it, and switch a system rule on or off.
- New ruleScreen
Build an automation rule on a full page: which records it watches, what starts it, which records qualify, and the ordered actions it carries out.
- RulesScreen
Every automation rule in your organization, system and custom, with filters, the switch that turns each rule on or off, and shortcuts to edit, test, clone, see the run history of or delete a rule.
- RunsScreen
Every time a rule ran: what started it, which record it worked on, how each action went, and the panel from which an errored run is retried.
- OutboxScreen
The events recorded when records change, on their way to the rules that listen to them, with the runs each event produced and the panel from which a failed event is requeued.
- Build and switch on a custom ruleHow-to
Build your own automation rule on the rule editor (what it watches, what starts it, which records qualify and what it does), save it switched off, try it on a real record with a dry run, then switch it on and check its runs.
- Customise a system ruleHow-to
Change what a ready-made system rule does (its timing, conditions, recipients or wording) by cloning it into your own rule, editing the copy, switching the copy on and the original off.
- Investigate a failed automationHow-to
When a reminder, task or automatic run didn't happen, check that the workflow engine is working, find the run that failed and read its error, fix the cause, then retry the run or requeue the event.
- Triggers and event typesConcept
The five ways a workflow rule starts (On Create, On Update, On Event, Scheduled, Manual), the catalogue of system event types the app raises, the daily due-date events for instalments and overdue demands, how recurrences are counted, and custom event types that chain rules.
- Workflow runs and the outboxConcept
How a change becomes automation: the outbox event written with the change, the pick-up that hands it to the engine, one run per listening rule with a recorded result per action, the statuses each passes through, and how Errored runs are retried and Failed events requeued.
- Workflow rule will not save: a trigger, schedule, condition or action is incomplete or not allowedTroubleshooting
Create rule or Save changes is refused when the trigger is missing its event type, schedule or input form, when a condition names a field the record doesn't have, when an action is switched off on this server or is set up in a way the engine doesn't allow, or when the rule has nothing to do.
- A run's action errored: webhook refused, empty role, empty reason, unpublished template or the chain ran too deepTroubleshooting
A workflow run stops as Errored when one of its actions fails: the webhook address is internal or the other system refused the call, the role to assign to has no active members, a cancellation or lost reason came out blank, or the document template is not published; a run is Blocked when a chain of rules emitting events runs deeper than the engine allows.
- Workflow rule, event type or message template cannot be deleted: runs, rules or actions use itTroubleshooting
A workflow rule that has run is kept for its history and a system rule is never deleted or edited; switch them off instead. A custom event type that rules listen to, a message template that workflow actions or campaigns use, cannot be deleted either; deactivate it, or change what uses it first.
- "Already exists" or "listed twice": the name, code, number or line must be uniqueTroubleshooting
Names, codes and numbers are unique within their scope (the organization, a project, a tower, a set of books) so that records can be told apart, and a line can appear once on a plan, offer or account; rename, reuse the existing record, or remove the duplicate line.