Record-Triggered Flow: Real-world Lead Qualification Automation

Overview

This post explains a real-world scenario where I used a Record-Triggered Flow in Salesforce to automate lead qualification and assignment. It covers the solution design, implementation details, which Flow trigger types I used (before-save vs after-save), and best practices for production-ready automation.

Business Problem

The sales operations team wanted leads to be automatically qualified and routed based on company size, industry, and geographic territory. Manual triage caused delays and inconsistent assignment. The requirements were:

  • Automatically set lead fields like Lead_Score__c and Territory__c when a lead is created or updated.
  • Assign the lead to the correct sales rep based on territory and load-balancing rules.
  • Create a follow-up task for the assigned rep and send a notification email when a high-priority lead appears.
  • Keep the solution bulk-safe and efficient to avoid hitting governor limits.

Why a Record-Triggered Flow?

Record-Triggered Flow is ideal because it runs declaratively on create/update events, supports both before-save & after-save execution contexts, and can handle record updates, create/delete operations, and external actions (like sending emails) without Apex. This fits the requirement to minimize code and enable easy maintenance.

Solution Design

The Flow was split into two parts to optimize performance:

1) Before-Save Flow (Fast Field Updates)

Used when: we need to set or calculate fields on the triggering record quickly without performing DML operations.

Key responsibilities:

  • Calculate Lead_Score__c based on number of employees, industry, and recent activity.
  • Derive Territory__c by mapping country/region to territory code.

Benefits: Before-save flows execute faster and consume fewer resources because they update the triggering record without extra DML. This keeps CPU and governor limits in check when many leads are processed.

2) After-Save Flow (Actions and Record Creation)

Used when: we need to create related records, perform complex assignments, or send emails.

Key responsibilities:

  • Assign the lead owner using a lookup to the User table and a custom load-balancing algorithm (round-robin stored in a custom setting).
  • Create a Task for the assigned sales rep with a 24-hour due date.
  • Send an email alert to the rep only if Lead_Score__c >= 80.

Flow Logic (Pseudo-code)


Trigger: Lead (on Create or Update)

Before-Save:
IF Industry in (Tech, Finance) THEN
Lead_Score__c += 20
IF Employees > 1000 THEN
Lead_Score__c += 30
Map Country to Territory__c
Save changes to Lead (fast update)

After-Save:
Get list of active reps for Territory__c
Owner = PickNextRepUsingRoundRobin(Territory__c)
UPDATE Lead.OwnerId = Owner.Id
CREATE Task: WhoId = Lead.Id, OwnerId = Owner.Id, Subject = "Follow up lead"
IF Lead_Score__c >= 80 THEN
Send Email to Owner

Important Implementation Details

  • Use Get Records with a limit and indexed fields (e.g., Territory__c indexed) to avoid full table scans.
  • For the round-robin logic, store the last-used rep in a Hierarchy Custom Setting or Custom Metadata and update it transactionally in the after-save flow.
  • Use a Single After-Save Flow element to create tasks in bulk: collect task records into a collection and perform one Create Records action.
  • Use fault paths to capture and log exceptions into a custom object (e.g., Automation_Error__c), and notify admins only on failures.

Testing & Validation

Thorough testing included:

  • Unit tests: Create multiple leads in bulk (200 records) to verify the before-save and after-save flows process without hitting limits.
  • Negative tests: Leads missing key fields should follow a safe default routing and generate an error log — not fail the transaction silently.
  • User acceptance testing: Sales reps validated task creation, owner assignment, and email notifications.

Results & Benefits

After deployment:

  • Lead routing time reduced from hours to seconds.
  • Consistent scoring and assignment; high-quality leads were followed up faster resulting in a measurable uplift in conversion.
  • Maintenance overhead decreased since business rules are in a declarative Flow (easy to update by admins).

Best Practices & Lessons Learned

  • Prefer before-save flows for simple field updates to reduce DML and execution time.
  • Use after-save flows for creating related records and external actions.
  • Keep flows modular: split complex logic into subflows for reuse and readability.
  • Always design for bulk — collect records into collections and use single DML actions.
  • Implement robust error handling with fault paths and admin notifications.

Summary

This record-triggered Flow delivered fast, maintainable automation for lead qualification and routing. By combining before-save and after-save flows, following bulkification patterns, and adding fault handling, the team achieved a reliable solution with minimal code.