Model Generation (CLI)¶
The sfc CLI tool automatically generates TypeScript model files from your Salesforce metadata. This is the recommended way to create models, as it ensures accuracy and saves time.
Installation¶
Install globally to use sfc commands directly:
Now you can use sfc anywhere without npx:
Alternative: If you prefer not to install globally, use npx instead:
Quick Start¶
-
Initialize configuration:
This creates a.sfconnect.jsonconfiguration file in your project root. -
Configure authentication by editing
.sfconnect.json:{ "instanceUrl": "https://your-instance.salesforce.com", "apiVersion": "v59.0", "authType": "jwt", "tokenUrl": "https://login.salesforce.com/services/oauth2/token", "clientId": "YOUR_CONNECTED_APP_CLIENT_ID", "username": "your-username@example.com", "privateKeyPath": "./certs/server.key", "algorithm": "RS256" } -
Test your authentication:
-
Generate models:
Generating Models¶
IMPORTANT: Always use the exact Salesforce API name for objects:
- ✅ Standard objects: Account, Contact, Opportunity, Lead, Case, User
- ✅ Custom objects: CustomObject__c, ProductReview__c (must include __c suffix)
- ❌ Don't use labels like "Accounts" or "Custom Object"
Generate to default location (./src/models):¶
Generate to custom directory:¶
# Output to ./models
sfc scaffold Account -o ./models
# Output to nested directory
sfc scaffold Contact Opportunity -o ./src/salesforce/models
# Generate custom object
sfc scaffold ProductReview__c -o ./models
Generate multiple objects at once:¶
Smart Incremental Scaffolding¶
As of v1.1.0, the scaffold command intelligently preserves your custom code when updating existing models!
How It Works¶
When you run sfc scaffold on an existing model file:
- ✅ Preserves custom methods - Your business logic, helpers, and custom functions stay intact
- ✅ Preserves custom interface properties - Relationship fields like
Owner?: UserDataare kept - ✅ Preserves custom imports - Related model imports remain in place
- ✅ Updates field definitions - Adds new fields from Salesforce metadata
- ✅ Updates field types - Fixes type mismatches (e.g.,
string→number) - ✅ Creates automatic backups - Original file backed up before modification
- ✅ Smart index.ts merging - Adds new exports while preserving custom ones
Scaffold Options¶
Default behavior (recommended):
Force regeneration (discards all custom code):
Skip backups:
What Gets Generated¶
For each Salesforce object, the CLI generates:
- ✅ TypeScript interface (e.g., AccountData)
- ✅ Model class extending LambdaModel<T>
- ✅ Getters for all fields
- ✅ Setters for updateable fields only (read-only fields excluded)
- ✅ JSDoc comments with field labels and metadata
- ✅ Proper TypeScript types based on Salesforce field types
- ✅ An index.ts file for convenient imports
Example output structure:
./models/
├── Account.ts
├── Contact.ts
├── Opportunity.ts
├── index.ts
└── (backup files when updating)
CLI Commands Reference¶
| Command | Description | Options |
|---|---|---|
sfc init |
Create .sfconnect.json config file |
-o, --output <path> - Config file path |
sfc scaffold <objects...> |
Generate TypeScript models from Salesforce metadata | -o, --output <dir> - Output directory (default: ./src/models)-c, --config <path> - Config file path--no-comments - Skip JSDoc comments--force - Force regenerate (overwrites custom code)--no-backup - Skip creating backup files |
sfc test-auth |
Test JWT authentication | -c, --config <path> - Config file path |
Using Generated Models¶
Once generated, import and use your models:
import { SalesforceConfig } from 'javascript-salesforce-connector';
import { Account, Contact } from './models';
// Initialize Salesforce config
SalesforceConfig.initialize({
instanceUrl: 'https://your-instance.salesforce.com',
apiVersion: 'v59.0'
});
SalesforceConfig.setAccessToken(token);
// Use the generated models
const accounts = await Account
.select(x => ({ Id: x.Id, Name: x.Name, Industry: x.Industry }))
.where(x => x.Industry === 'Technology')
.limit(10)
.get();
for (const account of accounts) {
console.log(account.Name, account.Industry);
}
Next Steps¶
- CLI Authentication Setup - Configure JWT bearer flow
- Defining Models - Manual model creation
- Lambda Queries - Query your models