sf-apex-best-practices

Enforce Apex coding conventions for class organization, error handling, null safety, and collections.

13|2|Updated Mar 18, 2026
One-click install
npx skills add https://github.com/jiten-singh-shahi/salesforce-claude-code --skill sf-apex-best-practices
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: sf-apex-best-practices
Source: https://github.com/jiten-singh-shahi/salesforce-claude-code/tree/main/.cursor/skills/sf-apex-best-practices
Command: npx skills add https://github.com/jiten-singh-shahi/salesforce-claude-code --skill sf-apex-best-practices

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Inconsistent Apex structure, weak error handling, null-safety gaps, and inefficient collection usage cause brittle, hard-to-maintain Salesforce code and production incidents; this Skill provides clear, actionable conventions to make Apex classes robust and maintainable.

Core Features & Use Cases

  • Class organization: Prescribed ordering for constants, statics, fields, constructors, public methods, private methods, and inner classes to improve readability and discoverability.
  • Error handling and exceptions: Patterns for domain-specific exceptions, precise catch scopes, and meaningful messages including SaveResult inspection for partial DML failures.
  • Null safety and collections: Guidance on null-safe navigation, map/list/set selection, and governor-limit-aware collection patterns and query-to-map idioms.
  • Use Cases: Use when writing or reviewing Apex service classes, bulk processing logic, integration callouts, or refactoring legacy Apex for production readiness.

Quick Start

Analyze this Apex class and recommend reorganization, domain-specific exception usage, SaveResult checks, null-safety fixes, and collection optimizations for production readiness.

Frequently Asked Questions about sf-apex-best-practices

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
What are the best practices for Apex class organization and error handling?▼

Apex best practices prescribe a specific ordering for constants, statics, fields, constructors, public methods, private methods, and inner classes, alongside domain-specific exception patterns and precise catch scopes for robust error handling.

How do I handle SaveResult validation and partial DML failures in Apex?▼

Handle SaveResult validation in Apex by inspecting SaveResult records for partial DML failures, applying meaningful error messages, and using domain-specific exception patterns to ensure production-ready error handling.

How do I write null-safe Apex code and optimize collection usage for governor limits?▼

Write null-safe Apex by applying null-safe navigation patterns, and optimize collection usage by selecting appropriate maps, lists, or sets with governor-limit-aware query-to-map idioms to prevent runtime errors.

Can I use these Apex conventions for bulk processing and integration callouts?▼

Yes, you can use these Apex conventions for bulk processing routines and integration callouts, as the guidance enforces bulkification and governor-limit-aware collection patterns suitable for production-ready service classes.

What is the best way to refactor legacy Apex code for production readiness?▼

The best way to refactor legacy Apex for production readiness is by reorganizing class structures, implementing domain-specific exceptions, adding SaveResult checks, fixing null-safety gaps, and optimizing collections to improve maintainability.