|
35 | 35 | <button type="submit">Submit</button> |
36 | 36 | </form> |
37 | 37 | ``` |
| 38 | +
|
| 39 | +## Bulk Importing Records from CSV |
| 40 | +
|
| 41 | +When a user wants to import records (e.g., contacts) from a CSV, follow this process: |
| 42 | +
|
| 43 | +### Step 1: Get the CSV File |
| 44 | +
|
| 45 | +Ask the user to upload their CSV file. They can attach it to the chat, and you will access it via mongo_store. |
| 46 | +
|
| 47 | +### Step 2: Analyze CSV and Target Table |
| 48 | +
|
| 49 | +1. Read the CSV (headers + sample rows) from Mongo |
| 50 | +2. Call erp_table_meta() to retrieve the full schema of the target table (e.g., crm_contact) |
| 51 | +3. Identify standard fields and the JSON details field for custom data |
| 52 | +
|
| 53 | +### Step 3: Propose Field Mapping |
| 54 | +
|
| 55 | +Create an intelligent mapping from CSV → table fields: |
| 56 | +
|
| 57 | +1. Match columns by name similarity |
| 58 | +2. Propose transformations where needed (e.g., split full name, normalize phone/email, parse dates) |
| 59 | +3. Map unmatched CSV columns into the appropriate *_details JSON field |
| 60 | +4. Suggest an upsert key for deduplication (e.g., contact_email) if possible |
| 61 | +
|
| 62 | +Present the mapping to the user in a clear format: |
| 63 | +``` |
| 64 | +CSV Column → Target Field (Transformation) |
| 65 | +----------------------------------------- |
| 66 | +Email → contact_email (lowercase, trim) |
| 67 | +Full Name → contact_first_name + contact_last_name (split on first space) |
| 68 | +Phone → contact_phone (format: remove non-digits) |
| 69 | +Company → contact_details.company (custom field) |
| 70 | +Source → contact_details.source (custom field) |
| 71 | +
|
| 72 | +Upsert key: contact_email (will update existing contacts with same email) |
| 73 | +``` |
| 74 | +
|
| 75 | +### Step 4: Validate and Adjust |
| 76 | +
|
| 77 | +Ask the user to confirm or modify, field mappings, transformations, upsert behavior, validation rules |
| 78 | +
|
| 79 | +### Step 5: Generate Python Script to Normalize the CSV |
| 80 | +Use python_execute() only to transform the uploaded file into a clean CSV whose columns exactly match the ERP table. Read from the Mongo attachment and write a new CSV: |
| 81 | +
|
| 82 | +```python |
| 83 | +import pandas as pd |
| 84 | +
|
| 85 | +SOURCE_FILE = "attachments/solar_root/leads_rows.csv" |
| 86 | +TARGET_TABLE = "crm_contact" |
| 87 | +OUTPUT_FILE = f"{{TARGET_TABLE}}_import.csv" |
| 88 | +
|
| 89 | +df = pd.read_csv(SOURCE_FILE) |
| 90 | +records = [] |
| 91 | +for _, row in df.iterrows(): |
| 92 | + full_name = str(row.get("Full Name", "")).strip() |
| 93 | + parts = full_name.split(" ", 1) |
| 94 | + first_name = parts[0] if parts else "" |
| 95 | + last_name = parts[1] if len(parts) > 1 else "" |
| 96 | + record = {{ |
| 97 | + "contact_first_name": first_name, |
| 98 | + "contact_last_name": last_name, |
| 99 | + "contact_email": str(row.get("Email", "")).strip().lower(), |
| 100 | + "contact_phone": str(row.get("Phone", "")).strip(), |
| 101 | + "contact_details": {{ |
| 102 | + "company": str(row.get("Company", "")).strip(), |
| 103 | + "source": "csv_import" |
| 104 | + }} |
| 105 | + }} |
| 106 | + records.append(record) |
| 107 | +
|
| 108 | +normalized = pd.DataFrame(records) |
| 109 | +normalized.to_csv(OUTPUT_FILE, index=False) |
| 110 | +print(f"Saved {{OUTPUT_FILE}} with {{len(normalized)}} rows") |
| 111 | +``` |
| 112 | +
|
| 113 | +python_execute automatically uploads generated files back to Mongo under their filenames (e.g., `crm_contact_import.csv`), so you can reference them with mongo_store or the new import tool. |
| 114 | +
|
| 115 | +### Step 6: Review the Normalized File |
| 116 | +1. Use `mongo_store(op="cat", args={{"path": "crm_contact_import.csv"}})` to show the first rows |
| 117 | +2. Confirm every column matches the ERP schema (no extras, correct casing) and the upsert key looks good |
| 118 | +3. Share stats (row count, notable transforms) with the user |
| 119 | +
|
| 120 | +### Step 7: Import with `erp_csv_import` |
| 121 | +
|
| 122 | +Use erp_csv_import() to import the cleaned CSV. |
| 123 | +
|
| 124 | +After import, offer to create follow-up tasks or automations for the new contacts. |
38 | 125 | """ |
39 | 126 |
|
40 | 127 | rick_prompt_default = f""" |
|
0 commit comments