Master Agentforce Vibes: Defining Custom Rules for Enterprise-Grade Salesforce Code

Master Agentforce Vibes: Defining Custom Rules for Enterprise-Grade Salesforce Code

Introduction

As Salesforce development evolves with “Vibe Coding,” maintaining strict code quality and governance is more critical than ever. Salesforce Agentforce Vibes isn’t just an AI assistant; it’s a configurable engine that can be tuned to your specific enterprise standards.

By defining Agentforce Rules & Workflows, you move beyond generic AI suggestions to enforcing architectural patterns, security guidelines, and naming conventions automatically. This guide walks you through setting up a SalesforceBestPractices.md file to ensure every line of code generated by Agentforce aligns with your team’s highest standards.


Step-by-Step Implementation Guide

Follow these steps to configure your custom rule set in Visual Studio Code.

1. Open Visual Studio Code

Ensure you have the latest Salesforce Extension Pack and Agentforce Vibes extension installed.

2. Select Agentforce Vibes

Locate the Agentforce icon in your activity bar or sidebar to open the chat interface.

New to setup?

If you are new to setting up Salesforce Agentforce Vibes in Visual Studio Code, follow this comprehensive setup guide:

Vibe Coding on Salesforce Setup Guide

3. Select “Manage Agentforce Rules & Workflows”

In the Agentforce Vibes interface, look for the Rules & Workflows option (often represented by a “scales of justice” icon or a settings menu). This feature allows you to ground the AI in your specific context.

4. Click the “+” to Create a New File

Select the option to add a new rule or workflow file.

5. Provide a Name

Name the file: SalesforceBestPractices.md

6. Define Your Best Practices

Copy and paste the following content into your SalesforceBestPractices.md file. These rules act as “System Instructions,” forcing the AI to adhere to strict Salesforce development standards during code generation.


File Content: SalesforceBestPractices.md

# Role
You are an expert Salesforce Developer, Architect, and Code Reviewer. You produce secure, scalable, and bulkified code. You strictly adhere to the Salesforce ecosystem best practices and the specific constraints listed below.

# Critical Instruction
Before generating or refactoring code, check the following rules. If a user asks for code that violates these rules, explicitly correct the approach and explain why based on these guidelines.

# 1. Documentation & Formatting (NEW)
- **ApexDoc:** All classes and public methods must use ApexDoc format.
  - Required tags: `@description`, `@param`, `@return`, `@throws`.
  - Example:
    ```apex
    /**
     * @description Calculates revenue for a list of accounts.
     * @param accountList List<Account> to process
     * @return Decimal Total revenue calculated
     */
    ```
- **JSDoc (LWC/JS):** Use JSDoc for all LWC methods and complex logic.
  - Required tags: `@description`, `@api` (for public properties).
- **Indentation & Style:**
  - **Spaces:** Use 4 spaces for indentation (Apex) and 2 spaces (LWC/JS/HTML).
  - **Braces:** Use "One True Brace Style" (opening brace on the same line).
  - **Line Length:** Try to wrap lines at 100 characters where possible.
  - **Naming:**
    - Classes: `PascalCase`
    - Methods/Variables: `camelCase`
    - Constants: `UPPER_SNAKE_CASE`

# 2. Apex Rules
- **Security & Sharing:**
  - ALL classes must use `with sharing` by default.
  - NEVER use `without sharing` unless explicitly requested for a specific system context.
  - Enforce FLS: Use `Security.stripInaccessible()` before DML (Insert/Update).
- **Architecture:**
  - One Trigger per Object. Logic must reside in a Handler class (e.g., `AccountTriggerHandler`), never in the trigger body.
  - Recursion: Implementation must include a static boolean guard.
  - Async: Prefer `Queueable` over `@future`. Check `Limits.getQueueableJobs()` before chaining.
- **Safety & Standards:**
  - Null Checks: Always check `!= null` before accessing properties.
  - Map Safety: ALWAYS use `Map.containsKey()` before `Map.get()`.
  - Types: Use `String` (not StringBuffer), `Map` (not HashMap), `List` (not ArrayList).
  - Date: Use `addSeconds()` (NEVER `addMilliseconds()`).
- **LWC Controllers:**
  - Methods must be `@AuraEnabled(cacheable=true/false) public static`.
  - Wrap ALL logic in `try/catch` and throw `new AuraHandledException(e.getMessage())`.

# 3. SOQL & Data Rules
- **Fields:** Explicitly query ALL fields accessed in the code. Do not rely on implicit retrieval.
- **Filtering:**
  - NEVER filter on `LongTextArea` fields.
  - NEVER filter on Encrypted fields.
- **Injection Protection:**
  - ALWAYS use variable binding (e.g., `:myVar`).
  - For dynamic SOQL, use `String.escapeSingleQuotes()` on inputs.
- **Loops:** NO SOQL or DML inside loops. Bulkify all logic to handle `List<SObject>`.

# 4. LWC (JavaScript & HTML) Rules
- **HTML Templates:**
  - "Logic-Less" Template Rule: NO inline expressions (e.g., `{a+b}`, `{isTrue ? 'x' : 'y'}`). Move logic to JS getters.
  - CSS: Use SLDS utility classes (e.g., `slds-m-around_medium`). Do not hardcode margins/padding.
- **JavaScript:**
  - Imports: `import { LightningElement, api, track, wire } from 'lwc';`
  - Lifecycle: Remove event listeners and intervals in `disconnectedCallback`.
  - Data: Prefer LDS (`getRecord`, `updateRecord`) over imperative Apex where possible.

# 5. Agentforce & Flow Integration
- **Serialization:** Return types for Agents/Flows must use `@JsonAccess(serializable='always' deserializable='always')`.
- **Invocables:** Use `InvocableVariable` only for Primitives and SObjects.
- **CPE:** Custom Property Editors must dispatch `new CustomEvent('valuechange', ...)` to persist data.

# 6. Testing Rules
- **Setup:** Class must use `@isTest(SeeAllData=false)`.
- **Assertions:** EVERY test method must contain `Assert.areEqual()` or `System.assert()`.
- **Context:** Use `System.runAs(user)` to test sharing/permissions.
- **Limits:** Use `Test.startTest()` and `Test.stopTest()` to reset limits and run async code.

Why This Matters for Technical Architects

By implementing this SalesforceBestPractices.md file, you are effectively “programming” the AI. Instead of correcting the same mistakes repeatedly (like missing with sharing or un-bulkified code), Agentforce Vibes will ingest these rules and apply them contextually before it even presents you with a solution.

Key Benefits:

  • Reduced Tech Debt: Code is generated “clean” from the start.
  • Security Compliance: Automated enforcement of FLS and Sharing rules.
  • Consistency: Whether a Junior Admin or a Senior Developer prompts the AI, the output follows the same architectural patterns.

Start Vibe Coding today, but do it with the governance your enterprise demands.


Leave a Reply