V4 - saveCreditRecordTool¶
This workflow saves and updates credit tracking records for users, managing their credit balances, recovery targets, and micro-action history in a PostgreSQL database. It handles both new credit records and updates to existing ones, automatically calculating locked credit amounts and weekly recovery progress.
Purpose¶
No business context provided yet — add a context.md to enrich this documentation.
How It Works¶
-
Receives Input Data: The workflow is triggered by another workflow with comprehensive credit tracking parameters including phone number, credit amounts, customer counts, and micro-action details.
-
Sanitizes and Encodes Parameters: All input fields are validated, sanitized (especially phone numbers), and encoded as Base64 JSON to prevent SQL injection and handle n8n's parameter limitations.
-
Updates Credit Record: Performs an upsert operation on the
creditTrackingtable, either creating a new record or updating an existing one based on the phone number. The operation includes complex calculations for:- Total credit locked (baseline minus recoveries plus new credit given)
- Weekly recovery progress tracking
- Micro-action date stamping
-
Logs Micro-Action History: If a micro-action outcome is provided, it creates a separate historical record in the
microActionHistorytable for audit and analysis purposes. -
Returns Summary: Outputs a formatted message showing the current credit status including total locked amount, weekly recovered amount, and current module stage.
Workflow Diagram¶
graph TD
A[When Executed by Another Workflow] --> B[encodeParams]
B --> C[upsertCreditRecord]
B --> D[logMicroActionHistory]
C --> E[setOutput]
Trigger¶
Execute Workflow Trigger: This workflow is designed to be called by other workflows within the same n8n instance. It accepts 18 different input parameters related to credit tracking and micro-actions.
Nodes Used¶
| Node Type | Node Name | Purpose |
|---|---|---|
| Execute Workflow Trigger | When Executed by Another Workflow | Receives input parameters from calling workflows |
| Code | encodeParams | Sanitizes input data and encodes as Base64 JSON for secure SQL execution |
| Postgres | upsertCreditRecord | Performs complex upsert operation on creditTracking table with credit calculations |
| Postgres | logMicroActionHistory | Inserts micro-action records into history table for tracking |
| Set | setOutput | Formats and returns a summary of the credit record operation |
External Services & Credentials Required¶
- PostgreSQL Database: Requires a configured Postgres credential in n8n with access to:
creditTrackingtable (read/write permissions)microActionHistorytable (write permissions)
Environment Variables¶
No specific environment variables are required for this workflow.
Data Flow¶
Input Parameters¶
phoneNumber(string): User's phone number (primary identifier)creditCustomersCount(number): Number of customers receiving credittotalCreditBaseline(number): Base credit amountcreditCustomerTypes(string): Types of credit customerscreditTypicalDays(number): Typical credit duration in daysweeklyRecoveryTarget(number): Target amount to recover weeklytargetCustomers(number): Target number of customerscreditGivenToday(number): Credit amount given todaycreditRecoveredToday(number): Credit amount recovered todaymicroActionType(string): Type of micro-action performedmicroActionScript(string): Script or description of micro-actionmicroActionAttempted(boolean): Whether micro-action was attemptedmicroActionOutcome(string): Result of the micro-actionamountRecovered(number): Amount recovered from micro-actionnextAction(string): Planned next actioncreditModuleStage(string): Current stage in credit modulecreditSkipReason(string): Reason for skipping credit actionsweekNumber(number): Current week number in program
Output¶
output(string): Formatted summary message containing:- Total credit locked amount in KES
- Weekly recovered amount in KES
- Current credit module stage
Error Handling¶
- Input Sanitization: Phone numbers are cleaned to remove non-numeric characters (except +)
- Null Handling: Undefined values are converted to null for proper database insertion
- Micro-Action Logging: The history logging node has
continueRegularOutputerror handling, allowing the workflow to complete even if history logging fails - Global Error Workflow: The workflow is configured with error workflow ID
cuHEGQjAfvuGwIODfor centralized error handling
Known Limitations¶
No specific limitations documented in the provided context.
Related Workflows¶
No related workflows specified in the current context.
Setup Instructions¶
-
Import the Workflow: Import the JSON into your n8n instance
-
Configure Database Connection:
- Set up a PostgreSQL credential in n8n
- Ensure the database contains the required tables:
creditTrackingwith all referenced columnsmicroActionHistorywith all referenced columns
-
Set Caller Policy: The workflow is configured to only accept calls from workflows owned by the same user (
workflowsFromSameOwner) -
Configure Error Handling:
- Set up error workflow with ID
cuHEGQjAfvuGwIODor update the error workflow reference - Alternatively, remove the error workflow setting if not needed
- Set up error workflow with ID
-
Test the Integration: Call this workflow from another workflow with sample credit tracking data to verify proper operation
-
Activate: Set the workflow to active status when ready for production use