diff --git a/src/lib/forms-shared.ts b/src/lib/forms-shared.ts index de5ff4b..978c481 100644 --- a/src/lib/forms-shared.ts +++ b/src/lib/forms-shared.ts @@ -198,6 +198,7 @@ export function applyVariables( homeowner?: HomeownerLite | null; firmName?: string; customValues?: Record; + clientCustomValues?: Record; fieldValues?: Record; }, ): string { @@ -215,11 +216,12 @@ export function applyVariables( let out = body; - // System variables: {{name}} or {{name|modifier}} + // Client custom-field variables: {{client.custom.key}} or {{client.custom.key|modifier}} + // Must run BEFORE the generic {{custom.key}} pattern so the prefix isn't swallowed. out = replaceWithModifier( out, - /\{\{\s*([a-zA-Z][a-zA-Z0-9_]*)\s*(\|\s*([a-zA-Z]+)\s*)?\}\}/g, - (name) => systemValues[name], + /\{\{\s*client\.custom\.([a-zA-Z0-9_]+)\s*(\|\s*([a-zA-Z]+)\s*)?\}\}/g, + (name) => ctx.clientCustomValues?.[name], ); // Custom case-field variables: {{custom.key}} or {{custom.key|modifier}} @@ -236,6 +238,13 @@ export function applyVariables( (name) => ctx.fieldValues?.[name], ); + // System variables: {{name}} or {{name|modifier}} — run LAST so dotted vars above win. + out = replaceWithModifier( + out, + /\{\{\s*([a-zA-Z][a-zA-Z0-9_]*)\s*(\|\s*([a-zA-Z]+)\s*)?\}\}/g, + (name) => systemValues[name], + ); + return out; }