AI-powered tools are revolutionizing how we build applications, and Salesforce development is no exception. Agentforce for Developers is an AI-powered tool that’s purpose-built for Salesforce projects, designed to help you generate code from natural language prompts. These tools excel at producing code quickly, but how can you control and guide the AI to generate consistent code? That’s where AI Customization Rules come in.
In this blog post and accompanying video, we will dive into this feature of Agentforce for Developers and discuss how to get the most from it.
Problem: Guiding AI-generated code
Imagine that you’re working on an Apex class and must generate a method to insert records. You open Dev Assistant (by clicking the Agentforce icon in the Activity bar) and provide your natural language prompt with method parameters and business logic.
Dev Assistant then processes your request and generates Apex code grounded in your org’s objects and fields. However, the generated code has SOQL and DML statements running in the system mode. While you could specify that AI should generate code to use user mode in your prompt every time, it’s not ideal for consistency or efficiency.
The screenshot below shows Dev Assistant generating code without any rules. Notice that the SOQL and the DML statements run in system mode.
The Solution: AI customization rules
AI Customization Rules allow you to define how AI-generated code should look and behave. By adding simple Markdown files in the .sfdx/a4d/rules
folder within your Salesforce DX project, you can instruct the AI model to adhere to your specific coding standards or preferred styles.
Each rule set includes a:
- Name: A clear identifier for the rule
- Description: Explaining the purpose of the rule
- File pattern it applies to: Using glob patterns (e.g.,
*.cls
for all Apex classes) - List of specific requirements: The guidelines that the AI should follow
For instance, you can define rules to ensure that all SOQL queries always use WITH USER_MODE
and all DML statements use user mode database operations. Once these rules are in place, generating the code again will result in output that respects your defined standards, with all database operations correctly set to user mode without needing to specify it in the prompt.
Here is how you will create the rule:
- Create the rules directory: In your Salesforce DX project root, create a directory:
.sfdx/a4d/rules/
.
- Define your rules: Inside this directory, create Markdown (
.md
) files for your rules. For example, you might haveapex.md
,html.md
, etc. A typical rule structure looks like this:
# Rule: Enforce User Mode for Apex
**Description:** Ensures all SOQL queries and DML operations in Apex classes run in user mode.
**Applies to:** `*.cls`
**Guidelines:**
- All SOQL queries must include `WITH USER_MODE`.
- All DML operations (insert, update, delete, undelete) must include `AS USER`.
When you generate code once the rules are in place, Agentforce for Developers will automatically detect and apply these rules when you use its AI features.
Note: AI Customization Rules are now on by default in the Agentforce for Developers extension. But you can also toggle AI Customization Rules on or off from your VS Code settings.
Supported file types
The power of AI Customization Rules extends beyond just Apex files. The feature supports AI customization rules for various file types, like HTML, CSS, and JavaScript, and for various scenarios. The following are some scenarios that fit well for AI Customization Rules.
- Test generation: Define rules for how Apex tests should be written, such as mandating the use of the newer
Assert
classes. Below is an example rule file that works well.
# Rule: Use Assert Class for Apex Tests
**Description:** Enforces the use of the newer Assert class for better test readability.
**Applies to:** `*Test.cls`
**Guidelines:**
- All assertions must use methods from the `Assert` class (e.g., `Assert.areEqual()`, `Assert.isTrue()`).
- LWC generation: Ensure that your Lightning web component templates follow a specific structure. For example, you can add a rule for how conditionals (
<template if:true>
) should be written in HTML.
# Rule: LWC Template Structure
**Description:** Maintain consistent structure in LWC HTML files.
**Applies to:** `**/*.html`
**Guidelines:**
- Group conditional rendering (`lwc:if`/`lwc:elseif`) elements together
- Include a comment block before each major section
- Avoid using `if:true` and `if:false` for conditional logic in templates. Instead Use `lwc:if`, `lwc:elseif`, and `lwc:else` directives for forward compatibility.
Note: It’s a good practice to define one rule per file type to keep things clear and maintainable.
Considerations and limitations
As of May 2025, rules are limited in length. We only accept rule files up to 1,000 tokens; anything beyond that gets truncated. We have documented all other known limitations and considerations in the official docs.
Conclusion
AI Customization Rules provide a significant step forward in making AI-generated code more reliable and aligned with your development practices. By setting up these rules, you can save time on manual corrections and ensure a higher standard of code quality from the outset.
To understand the full capabilities, limitations, and considerations for using AI Customization Rules, be sure to check out the official AI Customization Rules documentation.
Resources
About the author
Mohith Shrivastava is a Principal Developer Advocate at Salesforce with 14 years of experience building enterprise-scale products on the Salesforce Platform. Mohith is currently among the lead contributors on Salesforce Stack Exchange, a developer forum where Salesforce Developers can ask questions and share knowledge. You can follow him on LinkedIn.