From f91511b9276c9e859fc9582cd2e54e90c311210b Mon Sep 17 00:00:00 2001 From: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Date: Sun, 1 Feb 2026 23:03:19 -0500 Subject: [PATCH] feat(salesforce): add field rules for round robin routing (#27402) * feat(salesforce): add field rules for round robin skip - Add rrSkipFieldRules schema to appDataSchema in zod.ts - Implement applyFieldRules method in CrmService.ts for post-query filtering - Integrate field rules into getContacts method when forRoundRobinSkip=true - Add UI component for configuring field rules in EventTypeAppCardInterface - Add translations for new UI strings - Add comprehensive tests for field rules functionality Field rules allow users to specify conditions based on Salesforce record fields with 'ignore' or 'must_include' actions. Rules are evaluated with AND logic and gracefully handle missing fields by skipping those rules. Co-Authored-By: joe@cal.com * fix: add children prop to Section.SubSectionHeader for field rules Co-Authored-By: joe@cal.com * feat(salesforce): add edit button for field rules Co-Authored-By: joe@cal.com * feat(salesforce): apply field rules to GraphQL results Co-Authored-By: joe@cal.com * feat(salesforce): add Redis caching for field validation Co-Authored-By: joe@cal.com * fix(salesforce): remove Redis caching to fix ERR_INVALID_THIS error - Removed Redis caching for field validation that was causing ERR_INVALID_THIS errors - Simplified field rules filtering to try query directly and skip filtering if it fails - Restored ensureFieldsExistOnObject method for other uses (write to record) - Field rules now gracefully handle invalid fields by skipping filtering Co-Authored-By: joe@cal.com * chore: reformat getAttributes.ts Co-Authored-By: Claude Opus 4.5 * feat(salesforce): dynamically build GraphQL query for field rules with multi-edge iteration - Add buildDynamicAccountQuery to inject field rule fields into GraphQL query - Add passesFieldRules to evaluate ignore/must_include rules against UIAPI nodes - Validate field rules via getObjectFieldNames (in-memory + Redis cache) before branching - Remove applyFieldRulesToGraphQLRecords (used jsforce conn in GraphQL path) - Iterate all edges in Tiers 1 and 2 instead of only the first result - Rank Tier 3 accounts by contact count and fallback to next on field rule failure Co-Authored-By: Claude Opus 4.5 * feat(salesforce): add field rule routing trace steps Add trace calls alongside field rule info logs for routing visibility: - fieldRulesValidated: records validation result in CrmService - fieldRuleFilteredRecord: records when account is filtered at each tier - fieldRuleEvaluated: records individual rule evaluation details - allRecordsFilteredByFieldRules: records when SOQL records are all filtered Co-Authored-By: Claude Opus 4.5 * fix(salesforce): remove duplicate field validation and fix disabled Select options applyFieldRules now receives pre-validated rules from the early validation block instead of re-validating internally. Also adds the missing options prop to the disabled Select in the field rules UI. Co-Authored-By: Claude Opus 4.5 * feat(salesforce): include failed rule details in field rule trace steps Rename passesFieldRules to getFailingFieldRule so the caller receives the specific rule that caused filtering. The fieldRuleFilteredRecord trace step now includes failedRule with field, value, and action. Co-Authored-By: Claude Opus 4.5 * test(salesforce): add GraphQL path tests for field rules Cover field rule filtering across all three GraphQL resolution tiers (contact, account, related contacts) with ignore and must_include actions, multi-edge fallback, case-insensitivity, and dynamic query selection. Co-Authored-By: Claude Opus 4.5 * Abstract new field rules settings * Reduce extra SOQL call * fix(salesforce): fix type errors in CrmService and GraphQL client - Add missing import for getRedisService from @calcom/features/di/containers/Redis - Fix ensureFieldsExistOnObject to properly return Field[] instead of incomplete function - Use Array.from() instead of spread operator for Set and Map iterators to fix downlevelIteration errors Co-Authored-By: joe@cal.com --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 4.5 --- apps/web/public/static/locales/en/common.json | 5 + .../components/EventTypeAppCardInterface.tsx | 8 +- .../components/FieldRulesSettings.tsx | 238 ++++++++++ .../salesforce/lib/CrmService.test.ts | 293 ++++++++++++ .../app-store/salesforce/lib/CrmService.ts | 182 +++++++- .../lib/graphql/SalesforceGraphQLClient.ts | 420 +++++++++++++++--- .../__tests__/SalesforceGraphQLClient.test.ts | 216 +++++++++ .../lib/graphql/__tests__/urqlMock.ts | 176 ++++++++ .../tracing/SalesforceRoutingTraceService.ts | 47 ++ packages/app-store/salesforce/zod.ts | 14 + .../features/attributes/lib/getAttributes.ts | 232 +++++++--- 11 files changed, 1680 insertions(+), 151 deletions(-) create mode 100644 packages/app-store/salesforce/components/components/FieldRulesSettings.tsx diff --git a/apps/web/public/static/locales/en/common.json b/apps/web/public/static/locales/en/common.json index b8b484ef04..bd7307b197 100644 --- a/apps/web/public/static/locales/en/common.json +++ b/apps/web/public/static/locales/en/common.json @@ -3734,6 +3734,11 @@ "completed_onboarding": "Completed onboarding", "salesforce_on_cancel_write_to_event": "On cancelled booking, write to event record instead of deleting event", "salesforce_on_every_cancellation": "On every cancellation", + "salesforce_rr_skip_field_rules": "Field rules for round robin skip", + "salesforce_rr_skip_field_rule_ignore": "Ignore", + "salesforce_rr_skip_field_rule_must_include": "Must include", + "salesforce_field_name_placeholder": "Field name (e.g., Industry)", + "add_new_rule": "Add new rule", "report_issue": "Report issue", "report_issue_description": "You can submit a ticket on GitHub or upgrade your plan to receive real-time support with developer conferences", "open_issue": "Open Issue", diff --git a/packages/app-store/salesforce/components/EventTypeAppCardInterface.tsx b/packages/app-store/salesforce/components/EventTypeAppCardInterface.tsx index a12c8d5e78..0c9d81d7a3 100644 --- a/packages/app-store/salesforce/components/EventTypeAppCardInterface.tsx +++ b/packages/app-store/salesforce/components/EventTypeAppCardInterface.tsx @@ -17,7 +17,8 @@ import { Section } from "@calcom/ui/components/section"; import { showToast } from "@calcom/ui/components/toast"; import { SalesforceRecordEnum } from "../lib/enums"; -import type { appDataSchema } from "../zod"; +import type { appDataSchema, RRSkipFieldRule } from "../zod"; +import FieldRulesSettings from "./components/FieldRulesSettings"; import WriteToObjectSettings, { BookingActionEnum } from "./components/WriteToObjectSettings"; const EventTypeAppCard: EventTypeAppCardComponent = function EventTypeAppCard({ app, eventType, onAppInstallSuccess }) { @@ -46,6 +47,7 @@ const EventTypeAppCard: EventTypeAppCardComponent = function EventTypeAppCard({ const roundRobinSkipFallbackToLeadOwner = getAppData("roundRobinSkipFallbackToLeadOwner") ?? false; const onCancelWriteToEventRecord = getAppData("onCancelWriteToEventRecord") ?? false; const onCancelWriteToEventRecordFields = getAppData("onCancelWriteToEventRecordFields") ?? {}; + const rrSkipFieldRules = (getAppData("rrSkipFieldRules") ?? []) as RRSkipFieldRule[]; const { t } = useLocale(); @@ -435,6 +437,10 @@ const EventTypeAppCard: EventTypeAppCardComponent = function EventTypeAppCard({ + setAppData("rrSkipFieldRules", rules)} + /> ) : null} diff --git a/packages/app-store/salesforce/components/components/FieldRulesSettings.tsx b/packages/app-store/salesforce/components/components/FieldRulesSettings.tsx new file mode 100644 index 0000000000..7184e44f3b --- /dev/null +++ b/packages/app-store/salesforce/components/components/FieldRulesSettings.tsx @@ -0,0 +1,238 @@ +import { useState } from "react"; + +import { useLocale } from "@calcom/lib/hooks/useLocale"; +import { Button } from "@calcom/ui/components/button"; +import { InputField } from "@calcom/ui/components/form"; +import { Select } from "@calcom/ui/components/form"; +import { Section } from "@calcom/ui/components/section"; + +import type { RRSkipFieldRule } from "../../zod"; +import { RRSkipFieldRuleActionEnum } from "../../zod"; + +type FieldRuleAction = (typeof RRSkipFieldRuleActionEnum)[keyof typeof RRSkipFieldRuleActionEnum]; + +const FieldRulesSettings = ({ + fieldRules, + updateFieldRules, +}: { + fieldRules: RRSkipFieldRule[]; + updateFieldRules: (rules: RRSkipFieldRule[]) => void; +}) => { + const { t } = useLocale(); + + const actionOptions = [ + { label: t("salesforce_rr_skip_field_rule_ignore"), value: RRSkipFieldRuleActionEnum.IGNORE }, + { label: t("salesforce_rr_skip_field_rule_must_include"), value: RRSkipFieldRuleActionEnum.MUST_INCLUDE }, + ]; + + const [newRule, setNewRule] = useState<{ field: string; value: string; action: FieldRuleAction }>({ + field: "", + value: "", + action: RRSkipFieldRuleActionEnum.IGNORE, + }); + + const [editingIndex, setEditingIndex] = useState(null); + const [editingRule, setEditingRule] = useState<{ + field: string; + value: string; + action: FieldRuleAction; + } | null>(null); + + const startEditing = (index: number) => { + const rule = fieldRules[index]; + setEditingIndex(index); + setEditingRule({ field: rule.field, value: rule.value, action: rule.action }); + }; + + const cancelEditing = () => { + setEditingIndex(null); + setEditingRule(null); + }; + + const saveEditing = () => { + if (editingIndex === null || !editingRule) return; + if (!editingRule.field.trim() || !editingRule.value.trim()) return; + + const newRules = [...fieldRules]; + newRules[editingIndex] = { + field: editingRule.field.trim(), + value: editingRule.value.trim(), + action: editingRule.action, + }; + updateFieldRules(newRules); + cancelEditing(); + }; + + return ( + + + <> + + +
+
{t("field_name")}
+
{t("value")}
+
{t("action")}
+
+
+ + {fieldRules.map((rule, index) => { + const isEditing = editingIndex === index; + return ( +
+
+ {isEditing ? ( + + setEditingRule((prev) => (prev ? { ...prev, field: e.target.value } : null)) + } + size="sm" + className="w-full" + /> + ) : ( + + )} +
+
+ {isEditing ? ( + + setEditingRule((prev) => (prev ? { ...prev, value: e.target.value } : null)) + } + size="sm" + className="w-full" + /> + ) : ( + + )} +
+
+ {isEditing ? ( + opt.value === rule.action)} + isDisabled={true} + /> + )} +
+
+ {isEditing ? ( + <> +
+
+ ); + })} +
+
+ setNewRule({ ...newRule, field: e.target.value })} + /> +
+
+ setNewRule({ ...newRule, value: e.target.value })} + /> +
+
+