Skip to content
Back to projects

AI Automation

AI Lead Qualification & Follow-up

An n8n automation that qualifies service inquiries, drafts replies with Google Gemini, and updates a Google Sheets CRM. Validation checks, fixed scoring rules, and a separate error handler manage duplicate submissions, rejected drafts, and uncertain email outcomes.

  • n8n
  • Google Gemini
  • Gmail
  • Google Sheets
  • Data Tables
  • JavaScript
  • Lead Qualification
  • CRM Automation
  • Email Automation
  • Structured Output
  • Error Handling
Architecture of an n8n lead qualification workflow using Gemini, Gmail, Google Sheets, and a companion error handler.
Lead intake, qualification, email checks, and CRM updates, with separate paths for review and runtime failures.

Problem

A service inquiry needs several decisions before it becomes a useful lead: is the information complete, does the request fit the service, and what response should the person receive?

I built this personal project to automate that process. The harder part was handling incomplete submissions, repeated inquiries, unexpected AI output, and failures around email sending. An email might be accepted by Gmail even when the workflow cannot confirm the outcome, or a CRM update might fail after the send succeeds. Those cases needed explicit handling.

Objective

Build a lead qualification workflow that captures inquiries, applies consistent scoring rules, sends checked email replies, and maintains readable CRM records. When a case cannot proceed automatically, preserve the available evidence and make it visible for human review.

Approach

I designed, implemented, tested, and documented the workflow in n8n. Google Gemini extracts information from the inquiry and drafts a reply. JavaScript rules then calculate the lead score, determine the route, and control whether the email can reach Gmail.

Those rules still depend on some AI-extracted information, such as intent, so validation and human review remain necessary.

I separated the records by purpose. CRM_Leads contains the business-facing lead details, Review Queue holds cases needing attention, and lead_processing stores execution references, processing states, Gmail message IDs, and errors.

Implementation

Checking inquiries before processing

The workflow starts with an n8n form, normalizes the submitted values, and validates required input before calling Gemini. It checks the normalized email and message against previous processing records to block repeat inquiries.

I also handled corrected submissions separately. An invalid submission receives an audit key that allows the same email and message to proceed once the invalid field is corrected.

Qualifying the lead

Gemini extracts seven structured fields, including service category, intent, requirements, missing information, and confidence. A validation step checks the returned structure, types, allowed values, and consistency with the service, budget, and timeline selected in the form.

The scoring code assigns up to 10 points across budget fit, intent, timeline, and clarity. Leads route to HOT, WARM, COLD, NEEDS_INFO, or REVIEW. Low confidence and uncategorized requests go to review before the score thresholds are considered.

Checking and sending the reply

For leads that can proceed, Gemini drafts a response. Before Gmail runs, the workflow checks the recipient, output structure, length, HTML, links, and internal phrases. A rejected draft goes to the Review Queue.

After a successful Gmail send, the workflow stores the returned message ID, updates the processing state, and writes the CRM record. I checked the actual Gmail node output when mapping the message ID.

n8n lead qualification workflow connecting form validation, Gemini extraction, lead scoring, email checks, Gmail, and CRM updates.
Main workflow: from inquiry validation and lead scoring to email sending and CRM updates.View full image (new tab)

Handling failures around Gmail

A separate error workflow classifies failures as BEFORE_GMAIL, AT_GMAIL, AFTER_GMAIL, or UNKNOWN.

At Gmail, recognized rejection evidence produces SEND_FAILED. An ambiguous outcome produces SEND_UNCERTAIN and requires investigation. The handler has no automatic resend path.

A failure after Gmail is treated differently: processing can fail while the email remains marked EMAIL_SENT. An existing message ID is preserved; an unavailable ID is left unknown.

The handler looks up the lead by execution ID. If that ID is missing or no matching record is found, it creates an unresolved review entry instead of updating an approximate match. After writing a failure state, it reads the record back and checks the saved values. A mismatch creates a persistence-conflict review item.

n8n error handler with branches for failures before, during, and after Gmail, including state verification and review queue updates.
Error handler: classifies failures, checks saved state, and queues unresolved cases for review without automatically resending email.View full image (new tab)

Keeping the CRM readable

I kept runtime errors and processing details in lead_processing so the CRM could focus on the inquiry, qualification, and follow-up information. CRM timestamps use Asia/Karachi formatting, and Lead ID provides a connection back to the technical record.

Controlled outcomes, including rejected AI output and leads routed to REVIEW, write directly to the Review Queue. They do not depend on a runtime exception to become visible.

Results

In the end-to-end regression test, a lead scored 8 and followed the HOT route. Gmail returned a message ID, the workflow recorded COMPLETED and EMAIL_SENT, and a CRM_Leads row was created without invoking the error handler.

Additional checks covered duplicate submissions, corrected invalid input, malformed AI output, rejected email drafts, and manual review routing.

Failure tests covered missing or unmatched execution IDs and failures before, during, and after Gmail. The HTTP 401 test followed SEND_FAILED, while HTTP 503 followed SEND_UNCERTAIN. An UNKNOWN-stage test preserved an existing EMAIL_SENT state and Gmail message ID.

These were functional checks on a personal portfolio project. They establish the observed behavior of the tested cases; production reliability and business impact were not measured.

Challenges

Email uncertainty was the main design challenge. A send error does not always establish whether Gmail accepted the message, so uncertain outcomes require manual investigation. Human review does not automatically resolve or resend an inquiry.

Duplicate checking and record creation are separate operations. Concurrent submissions can therefore create a race condition; exactly-once processing or delivery is not guaranteed.

AI validation catches defined structural and content problems, but it cannot guarantee that every extracted detail or reply is correct. The error classifier also relies on exact node names, so relevant renames require its mapping to be updated.

The published workflows are configurable templates. Credentials, resource IDs, and the error-workflow connection must be set up and tested before use.

What I learned

Testing failures changed how I designed the workflow. I needed to track email status separately from processing status because a CRM failure could happen after a successful send.

I also learned to inspect real node outputs before mapping fields, validate AI responses beyond their expected format, and verify important state updates by reading them back.

Separating CRM records, review tasks, and processing history made the system easier to follow. Each record now has a clear purpose, and an unresolved case retains the information needed for investigation.