# 2026-05-27

## Duplicate Zoho record bug — fixed

Found and fixed a bug in `push_lead_to_zoho()` (`integrations/zoho.py`). When searching for existing leads by email, Zoho can return multiple records (duplicates). The code blindly picked `result["data"][0]` which could be the **oldest** duplicate, not the newest.

### The bug
- Lead `mary@speechlanguagelittles.com` had 2 Zoho records
- Old record (May 17): `Last_Name: "Speech, Language and Littles"` (invalid — company name in Last_Name field)
- New record (today): Mary Bell (correct)
- Code picked the old one → PUT to it got a 400 error (Zoho rejected invalid field state)

### Fix
- Added `_pick_newest()` helper that sorts by `Created_Time` descending using `max(records, key=lambda r: r.get("Created_Time", ""))`
- Both the email search and company fallback search now use it
- Deleted the old duplicate record `4315036000085495003` via Zoho DELETE API
- Only 1 clean record remains for that email

### Lesson for future
- Zoho search can return duplicates — always sort by recency
- Any place in code that does `result["data"][0]` after a Zoho search may have the same bug
- The `search_records()` in `shared/zoho/client.py` also does `result.get("data", [])` without sorting — callers should sort
