salesforce-sdk-patterns
Apply production-ready Salesforce jsforce patterns for TypeScript and Python. Use when implementing Salesforce integrations, refactoring SDK usage, or establishing team coding standards for Salesforce. Trigger with phrases like "salesforce SDK patterns", "jsforce best practices", "salesforce code patterns", "idiomatic salesforce", "salesforce typescript".
Best use case
salesforce-sdk-patterns is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
Apply production-ready Salesforce jsforce patterns for TypeScript and Python. Use when implementing Salesforce integrations, refactoring SDK usage, or establishing team coding standards for Salesforce. Trigger with phrases like "salesforce SDK patterns", "jsforce best practices", "salesforce code patterns", "idiomatic salesforce", "salesforce typescript".
Teams using salesforce-sdk-patterns should expect a more consistent output, faster repeated execution, less prompt rewriting.
When to use this skill
- You want a reusable workflow that can be run more than once with consistent structure.
When not to use this skill
- You only need a quick one-off answer and do not need a reusable workflow.
- You cannot install or maintain the underlying files, dependencies, or repository context.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/salesforce-sdk-patterns/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How salesforce-sdk-patterns Compares
| Feature / Agent | salesforce-sdk-patterns | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
Apply production-ready Salesforce jsforce patterns for TypeScript and Python. Use when implementing Salesforce integrations, refactoring SDK usage, or establishing team coding standards for Salesforce. Trigger with phrases like "salesforce SDK patterns", "jsforce best practices", "salesforce code patterns", "idiomatic salesforce", "salesforce typescript".
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
Related Guides
AI Agents for Coding
Browse AI agent skills for coding, debugging, testing, refactoring, code review, and developer workflows across Claude, Cursor, and Codex.
Best AI Skills for Claude
Explore the best AI skills for Claude and Claude Code across coding, research, workflow automation, documentation, and agent operations.
Cursor vs Codex for AI Workflows
Compare Cursor and Codex for AI coding workflows, repository assistance, debugging, refactoring, and reusable developer skills.
SKILL.md Source
# Salesforce SDK Patterns
## Overview
Production-ready patterns for jsforce (Node.js) and simple-salesforce (Python) — singleton connections, typed queries, error handling, and token refresh.
## Prerequisites
- Completed `salesforce-install-auth` setup
- Familiarity with async/await and TypeScript generics
- Understanding of Salesforce sObject model
## Instructions
### Step 1: Singleton Connection with Auto-Refresh
```typescript
// src/salesforce/connection.ts
import jsforce from 'jsforce';
let conn: jsforce.Connection | null = null;
export async function getConnection(): Promise<jsforce.Connection> {
if (conn?.accessToken) {
// Test if token is still valid
try {
await conn.identity();
return conn;
} catch {
conn = null; // Token expired, reconnect
}
}
conn = new jsforce.Connection({
loginUrl: process.env.SF_LOGIN_URL || 'https://login.salesforce.com',
version: '59.0', // Pin API version for stability
});
await conn.login(
process.env.SF_USERNAME!,
process.env.SF_PASSWORD! + process.env.SF_SECURITY_TOKEN!
);
return conn;
}
```
### Step 2: Typed sObject Interfaces
```typescript
// src/salesforce/types.ts
/** Standard Salesforce sObject base fields */
interface SObjectBase {
Id: string;
CreatedDate: string;
LastModifiedDate: string;
SystemModstamp: string;
IsDeleted: boolean;
}
export interface Account extends SObjectBase {
Name: string;
Industry?: string;
AnnualRevenue?: number;
NumberOfEmployees?: number;
Website?: string;
Phone?: string;
BillingCity?: string;
BillingState?: string;
OwnerId: string;
}
export interface Contact extends SObjectBase {
FirstName?: string;
LastName: string;
Email?: string;
Phone?: string;
AccountId?: string;
Title?: string;
Department?: string;
}
export interface Opportunity extends SObjectBase {
Name: string;
Amount?: number;
StageName: string;
CloseDate: string;
AccountId?: string;
Probability?: number;
ForecastCategory?: string;
}
export interface Lead extends SObjectBase {
FirstName?: string;
LastName: string;
Company: string;
Email?: string;
Status: string;
IsConverted: boolean;
}
```
### Step 3: Type-Safe Query Builder
```typescript
// src/salesforce/queries.ts
import { getConnection } from './connection';
import type { Account, Contact, Opportunity } from './types';
export async function queryAccounts(
filters?: { industry?: string; minRevenue?: number }
): Promise<Account[]> {
const conn = await getConnection();
let soql = `
SELECT Id, Name, Industry, AnnualRevenue, NumberOfEmployees,
Website, Phone, BillingCity, BillingState, OwnerId
FROM Account
`;
const conditions: string[] = [];
if (filters?.industry) {
conditions.push(`Industry = '${filters.industry.replace(/'/g, "\\'")}'`);
}
if (filters?.minRevenue) {
conditions.push(`AnnualRevenue >= ${filters.minRevenue}`);
}
if (conditions.length > 0) {
soql += ` WHERE ${conditions.join(' AND ')}`;
}
soql += ' ORDER BY Name ASC LIMIT 200';
const result = await conn.query<Account>(soql);
return result.records;
}
export async function queryContactsByAccount(
accountId: string
): Promise<Contact[]> {
const conn = await getConnection();
const result = await conn.query<Contact>(
`SELECT Id, FirstName, LastName, Email, Phone, Title, Department
FROM Contact
WHERE AccountId = '${accountId}'
ORDER BY LastName ASC`
);
return result.records;
}
export async function queryOpenOpportunities(): Promise<Opportunity[]> {
const conn = await getConnection();
const result = await conn.query<Opportunity>(
`SELECT Id, Name, Amount, StageName, CloseDate, AccountId, Probability
FROM Opportunity
WHERE IsClosed = false AND CloseDate >= TODAY
ORDER BY CloseDate ASC`
);
return result.records;
}
```
### Step 4: Error Handling Wrapper
```typescript
// src/salesforce/errors.ts
export class SalesforceError extends Error {
constructor(
message: string,
public readonly errorCode: string,
public readonly statusCode?: number,
public readonly fields?: string[]
) {
super(message);
this.name = 'SalesforceError';
}
}
export async function safeSfCall<T>(
operation: () => Promise<T>,
context?: string
): Promise<T> {
try {
return await operation();
} catch (err: any) {
const errorCode = err.errorCode || err.name || 'UNKNOWN_ERROR';
const fields = err.fields || [];
// Map Salesforce error codes to actionable messages
const messages: Record<string, string> = {
'INVALID_FIELD': `Invalid field name in query. Fields: ${fields.join(', ')}`,
'MALFORMED_QUERY': 'SOQL syntax error — check field names and WHERE clause',
'INVALID_TYPE': 'sObject type does not exist — use API names like Account, not Accounts',
'INSUFFICIENT_ACCESS_OR_READONLY': 'User lacks permission for this operation',
'ENTITY_IS_DELETED': 'Record has been deleted — check Recycle Bin',
'DUPLICATE_VALUE': 'Duplicate external ID or unique field value',
'FIELD_INTEGRITY_EXCEPTION': 'Field validation rule failed',
'STRING_TOO_LONG': 'Field value exceeds max length',
'REQUEST_LIMIT_EXCEEDED': 'Daily API limit exhausted — check org limits',
};
throw new SalesforceError(
messages[errorCode] || err.message || 'Unknown Salesforce error',
errorCode,
err.statusCode,
fields
);
}
}
```
### Step 5: Retry Logic for Transient Errors
```typescript
const RETRYABLE_ERRORS = [
'REQUEST_LIMIT_EXCEEDED',
'SERVER_UNAVAILABLE',
'UNABLE_TO_LOCK_ROW',
];
export async function withRetry<T>(
operation: () => Promise<T>,
maxRetries = 3,
baseDelayMs = 1000
): Promise<T> {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
return await operation();
} catch (err: any) {
const errorCode = err.errorCode || err.name;
if (attempt === maxRetries || !RETRYABLE_ERRORS.includes(errorCode)) {
throw err;
}
const delay = baseDelayMs * Math.pow(2, attempt - 1);
console.warn(`Retryable error ${errorCode}, attempt ${attempt}/${maxRetries}, waiting ${delay}ms`);
await new Promise(r => setTimeout(r, delay));
}
}
throw new Error('Unreachable');
}
```
## Output
- Type-safe jsforce connection singleton with auto-refresh
- Typed sObject interfaces for Account, Contact, Opportunity, Lead
- SOQL query builders with parameterized filters
- Error handling mapped to Salesforce error codes
- Retry logic for transient failures
## Error Handling
| Pattern | Use Case | Benefit |
|---------|----------|---------|
| `safeSfCall()` wrapper | All API calls | Maps error codes to human messages |
| `withRetry()` | Transient failures (`UNABLE_TO_LOCK_ROW`) | Automatic recovery |
| Typed queries | All SOQL | Catches field mismatches at compile time |
| Token refresh | Long-running processes | Prevents session expiration |
## Resources
- [jsforce API Reference](https://jsforce.github.io/document/)
- [Salesforce Error Codes](https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_calls_concepts_core_data_objects.htm)
- [SOQL Reference](https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql.htm)
## Next Steps
Apply patterns in `salesforce-core-workflow-a` for CRUD operations at scale.Related Skills
workhuman-sdk-patterns
Workhuman sdk patterns for employee recognition and rewards API. Use when integrating Workhuman Social Recognition, or building recognition workflows with HRIS systems. Trigger: "workhuman sdk patterns".
wispr-sdk-patterns
Wispr Flow sdk patterns for voice-to-text API integration. Use when integrating Wispr Flow dictation, WebSocket streaming, or building voice-powered applications. Trigger: "wispr sdk patterns".
windsurf-sdk-patterns
Apply production-ready Windsurf workspace configuration and Cascade interaction patterns. Use when configuring .windsurfrules, workspace rules, MCP servers, or establishing team coding standards for Windsurf AI. Trigger with phrases like "windsurf patterns", "windsurf best practices", "windsurf config patterns", "windsurfrules", "windsurf workspace".
windsurf-reliability-patterns
Implement reliable Cascade workflows with checkpoints, rollback, and incremental editing. Use when building fault-tolerant AI coding workflows, preventing Cascade from breaking builds, or establishing safe practices for multi-file AI edits. Trigger with phrases like "windsurf reliability", "cascade safety", "windsurf rollback", "cascade checkpoint", "safe cascade workflow".
webflow-sdk-patterns
Apply production-ready Webflow SDK patterns — singleton client, typed error handling, pagination helpers, and raw response access for the webflow-api package. Use when implementing Webflow integrations, refactoring SDK usage, or establishing team coding standards. Trigger with phrases like "webflow SDK patterns", "webflow best practices", "webflow code patterns", "idiomatic webflow", "webflow typescript".
vercel-sdk-patterns
Production-ready Vercel REST API patterns with typed fetch wrappers and error handling. Use when integrating with the Vercel API programmatically, building deployment tools, or establishing team coding standards for Vercel API calls. Trigger with phrases like "vercel SDK patterns", "vercel API wrapper", "vercel REST API client", "vercel best practices", "idiomatic vercel API".
vercel-reliability-patterns
Implement reliability patterns for Vercel deployments including circuit breakers, retry logic, and graceful degradation. Use when building fault-tolerant serverless functions, implementing retry strategies, or adding resilience to production Vercel services. Trigger with phrases like "vercel reliability", "vercel circuit breaker", "vercel resilience", "vercel fallback", "vercel graceful degradation".
veeva-sdk-patterns
Veeva Vault sdk patterns for REST API and clinical operations. Use when working with Veeva Vault document management and CRM. Trigger: "veeva sdk patterns".
vastai-sdk-patterns
Apply production-ready Vast.ai SDK patterns for Python and REST API. Use when implementing Vast.ai integrations, refactoring SDK usage, or establishing coding standards for GPU cloud operations. Trigger with phrases like "vastai SDK patterns", "vastai best practices", "vastai code patterns", "idiomatic vastai".
twinmind-sdk-patterns
Apply production-ready TwinMind SDK patterns for TypeScript and Python. Use when implementing TwinMind integrations, refactoring API usage, or establishing team coding standards for meeting AI integration. Trigger with phrases like "twinmind SDK patterns", "twinmind best practices", "twinmind code patterns", "idiomatic twinmind".
together-sdk-patterns
Together AI sdk patterns for inference, fine-tuning, and model deployment. Use when working with Together AI's OpenAI-compatible API. Trigger: "together sdk patterns".
techsmith-sdk-patterns
TechSmith sdk patterns for Snagit COM API and Camtasia automation. Use when working with TechSmith screen capture and video editing automation. Trigger: "techsmith sdk patterns".