---
name: sf-schema
description: |
Scaffold custom objects, fields, validation rules, permission sets, and other
schema metadata as SFDX source XML. Use when asked to create objects, custom
fields, permission sets, validation rules, or generate metadata XML. Activate
on mentions of "custom object", "custom field", "permission set", "validation
rule", "picklist", "lookup field", or "object-meta.xml".
license: Apache-2.0
compatibility: Requires Salesforce CLI (sf) v2+. Authenticated org needed for deployment.
metadata:
author: clientell
version: "1.0.0"
tags: salesforce, schema, metadata, permissions, custom-objects
# Claude Code specific
allowed-tools: Read,Write,Edit,Bash(sf *),Glob,Grep
context: fork
---
# Schema Design & Permission Management
You are a Salesforce schema and metadata specialist. Generate valid SFDX source format metadata.
## Custom Object
```xml
Invoices
AutoNumber
INV-{000000}
Deployed
Private
true
true
true
```
## Custom Fields
```xml
Amount__c
Currency
18
2
true
```
### Common Field Types
```xml
Text
255
LongTextArea
32768
5
Picklist
true
false
Activetrue
Inactivefalse
Lookup
Account
Invoices
Invoices
MasterDetail
Account
Invoices
Invoices
false
false
Checkbox
false
Date
DateTime
Number
18
0
Text
Account__r.Name & ' - ' & TEXT(Amount__c)
```
## Validation Rules
```xml
Amount_Must_Be_Positive
true
Amount__c <= 0
Amount__c
Amount must be greater than zero.
```
## Permission Sets
```xml
false
true
false
true
true
false
true
Invoice__c.Amount__c
true
true
Invoice__c.Status__c
true
true
```
## Custom Permissions (for Flow Bypass)
```xml
false
```
### Additional Field Types
**Formula Field:**
```xml
FullName__c
Text
FirstName__c & ' ' & LastName__c
BlankAsZero
```
**Rollup Summary Field** (master-detail only):
```xml
Total_Amount__c
Summary
LineItem__c.Amount__c
sum
LineItem__c.Order__c
```
**Geolocation Field:**
```xml
Location
true
6
```
**Global Value Set (Reusable Picklist):**
```xml
Industries
false
Technologyfalse
Healthcarefalse
```
### Relationship Types
| Type | Delete Behavior | Rollup Summary | Reparenting | Max per Object |
|------|----------------|----------------|-------------|----------------|
| Master-Detail | Cascade delete | Yes | Configurable | 2 |
| Lookup | Block/Clear/Restrict | No | N/A | 40 (25 std) |
| External Lookup | N/A | No | N/A | - |
| Hierarchical | N/A | No | N/A | 1 (User only) |
| Junction (M2M) | Two master-details | On both parents | - | - |
### Record Types
```xml
Enterprise
true
Enterprise Sales Process
For enterprise accounts
```
### Custom Metadata Types vs Custom Settings
| Feature | Custom Metadata Types | Custom Settings (Hierarchy) |
|---------|----------------------|---------------------------|
| Deployable | Yes (metadata) | No (data) |
| SOQL required | Yes (or getInstance) | No (getOrgDefaults) |
| User/Profile override | No | Yes |
| Counts against SOQL limit | Yes | No |
| Use for | Org config, mappings | User preferences |
### Permission Set Groups
```xml
Account_Manager
Opportunity_Editor
Report_Viewer
```
## SFDX Source Directory Structure
```
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
```
## Gotchas
- Max **40 custom relationships** per object (25 for standard objects)
- Formula fields **cannot reference** LongTextArea, RichTextArea, MultiSelectPicklist, or Encrypted fields
- Rollup Summary fields work **only on Master-Detail** relationships — not Lookups
- Global Value Sets **cannot be converted back** to local picklists once shared
- Encrypted Text fields are **not searchable or sortable**
- Record type-dependent picklists require **explicit value mapping** per record type
- Permission Set Groups **recalculate asynchronously** — changes may take minutes to apply
- Custom Metadata Type records **cannot be created/updated via DML in production** — deploy only
- Profiles are **notoriously merge-conflict-prone** — prefer Permission Sets for everything
## References
- [Schema Reference](references/schema-reference.md) — formula fields, rollup summaries, geolocation, Global Value Sets, record types, page layouts, FlexiPages, custom metadata, platform events, Big Objects, quick actions, custom labels
## Workflow
1. Understand object/field requirements
2. Generate SFDX source format XML files
3. Generate permission set for new objects/fields
4. Deploy schema first, then code that references it
5. Verify with: `sf org list metadata -m CustomObject --target-org myOrg`