transaction-deletion-import-logic-summary.md
When importing a CSV without a company_field column or with empty values, the system uses smart auto-detection:
"company" field (most common convention)
company exists that links to Company DocTypeFirst Company link field (custom fields)
No company field (DocTypes without company filtering)
company_field as None/emptydoctype_name,child_doctypes
Sales Order,Sales Order Item
Note,
Task,
Result:
Sales Order: Auto-detects "company" field → Filters by companyNote: No company field → Deletes all Note recordsTask: Has "company" field → Filters by companydoctype_name,company_field,child_doctypes
Sales Order,company,Sales Order Item
Sales Contract,primary_company,Sales Contract Item
Sales Contract,billing_company,Sales Contract Item
Note,,
Result:
Sales Order: Uses "company" field explicitlySales Contract (row 1): Uses "primary_company" fieldSales Contract (row 2): Uses "billing_company" field (separate row!)Note: No company field, deletes all recordsdoctype_name,company_field,child_doctypes
Customer Invoice,head_office,Customer Invoice Item
Customer Invoice,billing_company,Customer Invoice Item
Deletion Process:
WHERE head_office = 'ABC Company'WHERE billing_company = 'ABC Company'# 1. Read company_field from CSV (may be empty)
company_field = row.get("company_field", "").strip()
# 2. Auto-detect if not provided
if not company_field:
# Try "company" first
if exists("company" field linking to Company):
company_field = "company"
else:
# Check for other Company link fields
company_fields = get_all_company_link_fields()
if company_fields:
company_field = company_fields[0] # Use first
# else: company_field stays empty
# 3. Validate if company_field was provided/detected
if company_field:
if not is_valid_company_link_field(company_field):
skip_with_error()
# 4. Count documents
if company_field:
count = count(WHERE company_field = self.company)
else:
count = count(all records)
# 5. Store in To Delete list
append({
"doctype_name": doctype_name,
"company_field": company_field or None, # Store None if empty
"document_count": count
})
CSV:
doctype_name,company_field,child_doctypes
Sales Order,,
Auto-Detection:
company_field = "company"WHERE company = 'Test Company'CSV:
doctype_name,company_field,child_doctypes
Project Contract,,
Auto-Detection:
company_field = "contracting_company"WHERE contracting_company = 'Test Company'CSV:
doctype_name,company_field,child_doctypes
Note,,
Global Settings,,
Auto-Detection:
company_field = NoneCSV:
doctype_name,company_field,child_doctypes
Sales Contract,primary_company,Sales Contract Item
Sales Contract,billing_company,Sales Contract Item
No Auto-Detection:
CSV:
doctype_name,company_field,child_doctypes
Sales Order,,Sales Order Item
Sales Contract,billing_company,Sales Contract Item
Note,,
Result:
✅ Flexible: Supports auto-detection and explicit specification ✅ Safe: Validates all fields before processing ✅ Clear: Empty company_field means "delete all" ✅ Powerful: Can target specific company fields in multi-company setups ✅ Backward Compatible: Old CSVs (without company_field column) still work
Old CSV (without company_field):
doctype_name,child_doctypes
Sales Order,Sales Order Item
New System Behavior:
New CSV (with company_field):
doctype_name,company_field,child_doctypes
Sales Order,company,Sales Order Item
Benefits:
Generated for Transaction Deletion Record enhancement