#!/usr/bin/env python3 """Emit the email-template catalog as a pack-owned reference-data source (deployments/offline/runtime/reference-data/email-template-catalog.json) so it seeds to BOTH sandbox and PRODUCTION via the governed apply_pack_reference_data path (manifest.json), not just the sandbox baseline seed. Source of truth = migrated/*.records.json (regenerate via build_template_catalog.py). The governed Account approval set (Account_Approved / Account_Rejected / Account_Submitted_For_Approval_Notice / Account_Recalled + the Account_Email_Templates folder) is owned separately by approval-email-templates.json; this file therefore EXCLUDES folder_account_email_templates to avoid a duplicate reference-data key. The catalog's Account templates still reference that folder cross-file (the applier collects all sources into one record context). """ from __future__ import annotations import json from pathlib import Path HERE = Path(__file__).resolve().parent MIG = HERE / "migrated" OUT = HERE.parent.parent.parent / "deployments" / "offline" / "runtime" / "reference-data" / "email-template-catalog.json" SHARED_FOLDER_KEY = "folder_account_email_templates" # owned by approval-email-templates.json def main() -> int: folders = json.loads((MIG / "email_folders.records.json").read_text(encoding="utf-8")) templates = json.loads((MIG / "email_template_catalog.records.json").read_text(encoding="utf-8")) records: list[dict] = [] for f in folders: if f["_key"] == SHARED_FOLDER_KEY: continue records.append({ "key": f["_key"], "object": "ReportFolder", "match": {"DeveloperName": f["DeveloperName"], "Type": "Email"}, "fields": {"Name": f["Name"], "DeveloperName": f["DeveloperName"], "Type": "Email", "AccessType": "Shared"}, }) for t in templates: records.append({ "key": t["_key"], "object": "_System_EmailTemplate", "match": {"DeveloperName": t["DeveloperName"]}, "fields": { "Name": t["Name"], "DeveloperName": t["DeveloperName"], "Subject": t["Subject"], "Body": t["Body"], # HtmlValue is NOT NULL with no default; mirror Body for these text templates. "HtmlValue": t["Body"], "TemplateType": t.get("TemplateType", "text"), "IsActive": True, "FolderId": "{{ record.%s.id }}" % t["FolderKey"], }, }) payload = {"version": 1, "records": records} OUT.write_bytes((json.dumps(payload, indent=2, ensure_ascii=False) + "\n").encode("utf-8")) nf = sum(1 for r in records if r["object"] == "ReportFolder") nt = sum(1 for r in records if r["object"] == "_System_EmailTemplate") print(f"wrote {len(records)} records ({nf} folders + {nt} templates) -> {OUT.name}") return 0 if __name__ == "__main__": raise SystemExit(main())