LLM Mistakes in Apex & LWC: Salesforce Code Generation Rules
Large Language Models (LLMs) are transforming Salesforce code generation for Apex and LWC but frequently produce code that compiles but fails in production due to Salesforce-specific constraints like governor limits, SOQL querying rules, and template restrictions. Common mistakes include missing queried fields in SOQL, improper use of LWC template syntax, lack of null safety checks, and missing permission set configurations. Applying a tailored ruleset for prompting LLMs can dramatically improve code accuracy and prevent runtime errors. Salesforce teams can leverage these rules to enhance their AI-assisted development workflows and reduce debugging time.
- Always include accessed fields and relationship fields in SOQL queries.
- Avoid inline expressions and ternary operators in LWC templates; use JavaScript getters instead.
- Add null checks before accessing fields in Apex to prevent runtime exceptions.
- Ensure permission sets include field and class access for successful deployments.
- Use @AuraEnabled and @JsonAccess annotations properly for LWC and Agentforce integration.
LLM mistakes are very common but at the same time LLMs are transforming how we write Salesforce code. Vibe Coding Tools use these LLMs under the hood to generate code suggestions for APEX and LWC. But here’s what most developers don’t realize: LLMs are making mistakes and don’t understand Salesforce’s unique constraints —governor limits, LWC template restrictions, or Agentforce-specific patterns. They generate code that compiles in your IDE but breaks in production . After months of prompting LLMs for Apex, LWC, and Flow code—and debugging the failures—I’ve documented the common mistakes LLMs make when generating Salesforce code. The Solution: Use this guide as a ruleset for your LLM prompts . Add these rules to your system prompts, custom instructions, or paste them directly when asking LLMs to generate Salesforce code. This will dramatically improve the quality of generated code.