Skip to content

V4 - saveOnboardingBaselineTool

A workflow tool that saves or updates baseline business information for youth entrepreneurs during their onboarding process. This workflow accepts key business metrics like fixed costs, operating days, and stock type, then stores them in a PostgreSQL database with smart conflict resolution.

Purpose

No business context provided yet — add a context.md to enrich this documentation.

How It Works

  1. Receives Input Parameters: The workflow accepts seven key parameters including phone number (as identifier), preferred contact time, monthly fixed costs, business days per month, daily fixed cost, stock type, and practice baseline average
  2. Builds Dynamic SQL: A JavaScript code node analyzes the input and constructs an UPDATE SQL query only for fields that have actual values (ignoring null, undefined, or empty strings)
  3. Validates Query: An IF node checks whether a valid SQL query was generated
  4. Executes Database Update: If there's a valid query, it executes the UPDATE statement against the PostgreSQL database
  5. Returns Response: Formats a success response with appropriate messaging

Workflow Diagram

graph TD
    A[Trigger] --> B[Code in JavaScript]
    B --> C[hasQuery]
    C -->|Has Query| D[executeBaselineSQL]
    C -->|No Query| E[formatResponse]
    D --> E

Trigger

Execute Workflow Trigger: Accepts the following input parameters: - phoneNumber - Unique identifier for the youth entrepreneur - preferredTime - Preferred contact time - monthlyFixedCosts - Monthly fixed business expenses - businessDaysMonth - Number of business days per month - dailyFixedCost - Daily fixed cost calculation - stockType - Type of stock/inventory managed - practiceBaselineAvg - Practice baseline average metric

Nodes Used

Node Type Node Name Purpose
Execute Workflow Trigger Trigger Receives input parameters from calling workflow
Code (JavaScript) Code in JavaScript Builds dynamic UPDATE SQL query based on provided fields
IF hasQuery Validates whether a SQL query was generated
Postgres executeBaselineSQL Executes the UPDATE query against the database
Set formatResponse Formats the final response with success status and message

External Services & Credentials Required

  • PostgreSQL Database: Requires connection to a PostgreSQL instance with the youthEntrepreneursReal table
  • Database Credentials: PostgreSQL connection credentials (host, port, database, username, password)

Environment Variables

No specific environment variables are referenced in this workflow. Database connection details are managed through n8n's credential system.

Data Flow

Input:

1
2
3
4
5
6
7
8
9
{
  "phoneNumber": "string (required)",
  "preferredTime": "string (optional)",
  "monthlyFixedCosts": "number (optional)",
  "businessDaysMonth": "number (optional)",
  "dailyFixedCost": "number (optional)",
  "stockType": "string (optional)",
  "practiceBaselineAvg": "number (optional)"
}

Output:

1
2
3
4
{
  "success": true,
  "message": "Baseline saved."
}

Error Handling

  • Database Errors: The executeBaselineSQL node is configured with "Continue on Error" to prevent workflow failure
  • Missing Phone Number: The JavaScript code validates phone number presence and returns appropriate error messaging
  • Empty Updates: If no valid fields are provided for update, the workflow returns a success message indicating no fields to update

Known Limitations

  • Phone number is required as the primary identifier - workflow will fail gracefully if not provided
  • The workflow only updates existing records; it doesn't create new ones if the phone number doesn't exist
  • Field validation is basic - relies on JavaScript type checking and null/undefined detection

This workflow is designed to be called by other workflows as a sub-workflow tool. The error workflow cuHEGQjAfvuGwIOD is configured to handle any critical failures.

Setup Instructions

  1. Import Workflow: Import the JSON into your n8n instance
  2. Configure PostgreSQL Credentials:
    • Create PostgreSQL credentials in n8n
    • Ensure connection to database with youthEntrepreneursReal table
  3. Database Schema: Ensure the target table has these columns:
    • phoneNumber (TEXT, primary key)
    • preferredTime (TEXT)
    • monthlyFixedCosts (NUMERIC)
    • businessDaysPerMonth (INTEGER)
    • dailyFixedCost (NUMERIC)
    • stockType (TEXT)
    • practiceBaselineAvg (NUMERIC)
  4. Test the Workflow: Execute with sample data to verify database connectivity
  5. Configure Error Workflow: Ensure the referenced error workflow (cuHEGQjAfvuGwIOD) exists or update the setting
  6. Set Caller Policy: The workflow is configured to accept calls from workflows owned by the same user