关于 sf-schema
面向 Salesforce 开发者和元数据专家的架构设计技能,指导生成对象、字段、规则和权限的 SFDX 源格式元数据,并说明关系限制与部署注意事项。
主要功能
- 01生成自定义对象元数据
- 02定义字段和验证规则
- 03配置权限与记录类型
- 04说明关系限制
- 05概述 SFDX 部署流程
适用场景
- 01创建对象及其字段
- 02为新架构设置权限
- 03检查关系与公式限制
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ clientell-ai/ salesforce-skills --skill sf-schema安装后在 Claude Code 中输入/sf-schema确认已加载。
附带文件references/schema-reference.md
详细介绍
架构设计与权限管理
你是 Salesforce 架构和元数据专家。生成有效的 SFDX 源格式元数据。
自定义对象
<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
<label>Invoice</label>
<pluralLabel>Invoices</pluralLabel>
<nameField>
<label>Invoice Number</label>
<type>AutoNumber</type>
<displayFormat>INV-{000000}</displayFormat>
</nameField>
<deploymentStatus>Deployed</deploymentStatus>
<sharingModel>Private</sharingModel>
<enableActivities>true</enableActivities>
<enableHistory>true</enableHistory>
<enableReports>true</enableReports>
</CustomObject>
自定义字段
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Amount__c</fullName>
<label>Amount</label>
<type>Currency</type>
<precision>18</precision>
<scale>2</scale>
<required>true</required>
</CustomField>
常见字段类型
<type>Text</type>
<length>255</length>
<type>LongTextArea</type>
<length>32768</length>
<visibleLines>5</visibleLines>
<type>Picklist</type>
<valueSet>
<restricted>true</restricted>
<valueSetDefinition>
<sorted>false</sorted>
<value><fullName>Active</fullName><default>true</default><label>Active</label></value>
<value><fullName>Inactive</fullName><default>false</default><label>Inactive</label></value>
</valueSetDefinition>
</valueSet>
<type>Lookup</type>
<referenceTo>Account</referenceTo>
<relationshipLabel>Invoices</relationshipLabel>
<relationshipName>Invoices</relationshipName>
<type>MasterDetail</type>
<referenceTo>Account</referenceTo>
<relationshipLabel>Invoices</relationshipLabel>
<relationshipName>Invoices</relationshipName>
<reparentableMasterDetail>false</reparentableMasterDetail>
<writeRequiresMasterRead>false</writeRequiresMasterRead>
<type>Checkbox</type>
<defaultValue>false</defaultValue>
<type>Date</type>
<type>DateTime</type>
<type>Number</type>
<precision>18</precision>
<scale>0</scale>
<type>Text</type>
<formula>Account__r.Name & ' - ' & TEXT(Amount__c)</formula>
验证规则
<?xml version="1.0" encoding="UTF-8"?>
<ValidationRule xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Amount_Must_Be_Positive</fullName>
<active>true</active>
<errorConditionFormula>Amount__c <= 0</errorConditionFormula>
<errorDisplayField>Amount__c</errorDisplayField>
<errorMessage>Amount must be greater than zero.</errorMessage>
</ValidationRule>
权限集
<?xml version="1.0" encoding="UTF-8"?>
<PermissionSet xmlns="http://soap.sforce.com/2006/04/metadata">
<label>Invoice Manager</label>
<hasActivationRequired>false</hasActivationRequired>
<objectPermissions>
<object>Invoice__c</object>
<allowCreate>true</allowCreate>
<allowDelete>false</allowDelete>
<allowEdit>true</allowEdit>
<allowRead>true</allowRead>
<modifyAllRecords>false</modifyAllRecords>
<viewAllRecords>true</viewAllRecords>
</objectPermissions>
<fieldPermissions>
<field>Invoice__c.Amount__c</field>
<editable>true</editable>
<readable>true</readable>
</fieldPermissions>
<fieldPermissions>
<field>Invoice__c.Status__c</field>
<editable>true</editable>
<readable>true</readable>
</fieldPermissions>
</PermissionSet>
自定义权限(用于绕过 Flow)
<?xml version="1.0" encoding="UTF-8"?>
<CustomPermission xmlns="http://soap.sforce.com/2006/04/metadata">
<label>Bypass Automation</label>
<isLicensed>false</isLicensed>
</CustomPermission>
其他字段类型
公式字段:
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>FullName__c</fullName>
<label>Full Name</label>
<type>Text</type>
<formula>FirstName__c & ' ' & LastName__c</formula>
<formulaTreatBlanksAs>BlankAsZero</formulaTreatBlanksAs>
</CustomField>
汇总汇总字段(仅限主从关系):
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Total_Amount__c</fullName>
<label>Total Amount</label>
<type>Summary</type>
<summarizedField>LineItem__c.Amount__c</summarizedField>
<summaryOperation>sum</summaryOperation>
<summaryForeignKey>LineItem__c.Order__c</summaryForeignKey>
</CustomField>
地理位置字段:
<type>Location</type>
<displayLocationInDecimal>true</displayLocationInDecimal>
<scale>6</scale>
全局值集(可复用选项列表):
<GlobalValueSet xmlns="http://soap.sforce.com/2006/04/metadata">
<masterLabel>Industries</masterLabel>
<sorted>false</sorted>
<customValue><fullName>Technology</fullName><default>false</default><label>Technology</label></customValue>
<customValue><fullName>Healthcare</fullName><default>false</default><label>Healthcare</label></customValue>
</GlobalValueSet>
关系类型
| 类型 | 删除行为 | 汇总汇总 | 重新指定父记录 | 每个对象的最大数量 |
|---|---|---|---|---|
| 主从关系 | 级联删除 | 是 | 可配置 | 2 |
| 查找关系 | 阻止/清除/限制 | 否 | 不适用 | 40(标准对象为 25) |
| 外部查找关系 | 不适用 | 否 | 不适用 | - |
| 层级关系 | 不适用 | 否 | 不适用 | 1(仅限 User) |
| 联结对象(M2M) | 两个主从关系 | 在两个父对象上 | - | - |
记录类型
<RecordType xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Enterprise</fullName>
<label>Enterprise</label>
<active>true</active>
<businessProcess>Enterprise Sales Process</businessProcess>
<description>For enterprise accounts</description>
</RecordType>
自定义元数据类型与自定义设置的对比
| 特性 | 自定义元数据类型 | 自定义设置(层级) |
|---|---|---|
| 可部署 | 是(元数据) | 否(数据) |
| 是否需要 SOQL | 是(或 getInstance) | 否(getOrgDefaults) |
| 用户/配置文件覆盖 | 否 | 是 |
| 是否计入 SOQL 限制 | 是 | 否 |
| 用途 | 组织配置、映射 | 用户偏好设置 |
权限集组
<PermissionSetGroup xmlns="http://soap.sforce.com/2006/04/metadata">
<label>Sales Team</label>
<permissionSets>
<permissionSet>Account_Manager</permissionSet>
<permissionSet>Opportunity_Editor</permissionSet>
<permissionSet>Report_Viewer</permissionSet>
</permissionSets>
</PermissionSetGroup>
SFDX 源目录结构
force-app/main/default/
├── objects/
│ └── Invoice__c/
│ ├── Invoice__c.object-meta.xml
│ ├── fields/
│ │ ├── Amount__c.field-meta.xml
│ │ └── Status__c.field-meta.xml
│ ├── validationRules/
│ │ └── Amount_Must_Be_Positive.validationRule-meta.xml
│ └── listViews/
│ └── All.listView-meta.xml
├── permissionsets/
│ └── Invoice_Manager.permissionset-meta.xml
├── customPermissions/
│ └── Bypass_Automation.customPermission-meta.xml
└── layouts/
└── Invoice__c-Invoice Layout.layout-meta.xml
注意事项
- 每个对象最多 40 个自定义关系(标准对象为 25 个)
- 公式字段无法引用 LongTextArea、RichTextArea、MultiSelectPicklist 或加密字段
- 汇总汇总字段仅适用于主从关系,不适用于查找关系
- 全局值集一旦共享,无法转换回本地选项列表
- 加密文本字段无法搜索或排序
- 依赖记录类型的选项列表需要为每种记录类型显式映射值
- 权限集组会异步重新计算,更改可能需要几分钟才能生效
- 生产环境中无法通过 DML 创建或更新自定义元数据类型记录,只能部署
- 配置文件极易产生合并冲突,所有情况都应优先使用权限集
参考资料
- 架构参考 — 公式字段、汇总汇总、地理位置、全局值集、记录类型、页面布局、FlexiPages、自定义元数据、平台事件、大对象、快速操作、自定义标签
工作流
- 了解对象/字段需求
- 生成 SFDX 源格式 XML 文件
- 为新对象/字段生成权限集
- 先部署架构,再部署引用该架构的代码
- 使用以下命令验证:
sf org list metadata -m CustomObject --target-org myOrg
评论
支持 Markdown