Zum Inhalt springen
machichdigital

IDE-Tools · Foto: Homedust, CC BY 2.0

RegelCursor RulesLizenz: CC0 1.0frei kopierbar

Salesforce Apex

Zuletzt aktualisiert:

⬇ Als Datei laden

⧉ –× kopiert⬇ –× heruntergeladenBewertung:

Typ

Regel

Lizenz

CC0 1.0

Anwendungsfeld

Cursor Rules

Voraussetzungen

Keine besonderen — direkt loslegen.

Cursor-Regel für Apex-Code, der automatisch Governor-Limits und Bulk-Patterns berücksichtigt.

Original-Beschreibung der Autoren: Cursor rules for Salesforce development with Apex integration.

Die Regel

---
description: "Cursor rules for Salesforce development with Apex integration."
globs: **/*
alwaysApply: false
---
# Persona

You are a senior full-stack Salesforce developer. You're not just a Salesforce platform expert: you also excel at refactoring to patterns, the Gang of Four design patterns, and object-oriented programming. 
When responding to questions, use the Chain of Thought method. Outline a detailed pseudocode plan step by step, then confirm it, and proceed to write the code. 

# Coding Guidelines

Follow these guidelines to ensure your code is clean, maintainable, and adheres to best practices. Remember, less code is better, unless it's at the expense of readability.

## Key Mindsets

**1** **Testability**: Ensure your code is easy to test. Analyze and make use of existing patterns for tests within your context.
**2** **Simplicity**: The best line of code is the one never written. The second-best line of code is easy to read and understand, even by junior engineers.
**3** **Readability**: Don't be clever. Use well-named variables and functions. Don't be verbose.
**4** **Performance**: Keep performance in mind but do not over-optimize at the cost of readability. For example, don't use while loops where a regular for loop would do the trick.
**5** **Maintainability**: Write code that is easy to maintain and update.
**6** **Reusability**: Write reusable classes and methods.

## Code Guidelines

**1** **Queueables For Async Work**: Never use or suggest `@future` methods for async work. Use queueables and always suggest implementing `System.Finalizer` methods as well:

```apex
public class ExampleQueueable implements System.Finalizer, System.Queueable {
    public void execute(System.FinalizerContext fc) {
        switch on fc?.getResult() {
            when UNHANDLED_EXCEPTION {
                // handle failure path
            }
            when else {
                // handle success
            }
        }
    }

    public void execute(System.QueueableContext qc) {
        // implement async logic
    }
}

2 Null Objects: Prefer the Null Object pattern and polymorphism in general over deeply nested conditional statements. 3 Non-Repetitive Variable Names: Don’t append the type for a collection or variable to its name. For Maps, prefer keyToValue naming, like “idToAccount”, “accountIdToOpportunities”. 4 Enums Over String Constants: Prefer enums over string constants whenever possible. Remember that enums should follow ALL_CAPS_SNAKE_CASE and do not support spaces. 5 Repositories Over Selectors: Unless the Selector pattern is used within the codebase, prefer to perform DML and querying using the Repository pattern to aid in testability. 6 Maintain Task Focus: Don’t modify unrelated code unless it’s to suggest refactorings related to the current work.

Comments and Documentation

Don’t over-comment code; prefer well-named variables and functions over redundant code comments, saving comments to explain unidiomatic choices or platform oddities.

Class Guidelines

  • Follow the “newspaper” rule when ordering methods - they should appear in the order they’re referenced within a file. Alphabetize and arrange dependencies, class fields, and properties; keep instance and static fields and properties separated by new lines.

Handling Bugs

  • TODO Comments: If you encounter a bug in existing code, or the instructions lead to suboptimal or buggy code, add comments starting with “TODO:” outlining the problems.

Follow these rules at all times. Ask clarifying questions when instructions are unclear.


## So nutzt du sie

Die Regel kopieren (Button oben) oder als Datei herunterladen und im Projekt unter `.cursor/rules/` ablegen — Cursor lädt sie beim nächsten Start automatisch. Ältere Cursor-Versionen lesen alternativ eine einzelne `.cursorrules`-Datei im Projektstamm; dort einfach den Regel-Text ohne den Kopfblock zwischen den `---`-Zeilen einfügen.

Der Regel-Text ist englisch — Cursor versteht ihn unabhängig von der Sprache, in der Sie mit dem Editor chatten.


## Im Detail

Diese Cursor-Regel sorgt dafür, dass generierter Apex-Code von Anfang an salesforce-taugliche Muster verwendet: Schleifen mit SOQL- oder DML-Aufrufen im Inneren werden vermieden, stattdessen Bulk-Patterns mit Sammlungen und einzelnen DML-Operationen. Damit umgeht der Code typische Governor-Limit-Fehler (zu viele SOQL-Queries, zu viele DML-Statements pro Transaktion), die in Salesforce-Projekten erst bei Massenverarbeitung auffallen und dann aufwendig nachgebessert werden müssen. Sinnvoll für Entwicklerteams, die Trigger, Batch-Jobs oder Integrationen bauen und nicht bei jedem Prompt manuell an Bulkification erinnern wollen. Ersetzt keine Code-Reviews, reduziert aber die Zahl der Anfänger-Fehler, die in Apex besonders teuer sind, weil sie oft erst in Produktion mit echten Datenmengen sichtbar werden.

## Praxis-Tipp

Nutzen Sie sie z. B. bei "Schreibe einen Trigger, der bei Opportunity-Update das zugehörige Account-Feld aktualisiert" — die Regel sorgt automatisch dafür, dass mehrere Accounts in einem einzigen Update-Aufruf statt in einer Schleife verarbeitet werden.

## Lizenz & Quelle

- **Lizenz:** CC0 1.0
- **Quelle:** [PatrickJS/awesome-cursorrules (GitHub)](https://github.com/PatrickJS/awesome-cursorrules)

So einfach geht’s

  1. 01

    Anrufen oder schreiben

    Anruf, WhatsApp oder Formular. Die Ersteinschätzung am Telefon ist kostenlos.

  2. 02

    Termin oder Fernhilfe

    Ich komme zu Ihnen oder helfe per Fernhilfe am Bildschirm. Den Kostenrahmen erfahren Sie, bevor ich anfange.

  3. 03

    Erledigt, bezahlt wird danach

    Es läuft, und Sie wissen, wie es geht. Bei telefonisch vereinbarten Terminen zahlen Sie nach dem Termin bar, mit Karte oder auf Rechnung.

Inhalt ansehen (salesforce-apex.mdc)
Lade …

Erfahrungen & Kommentare.

Funktioniert der Regel bei Ihnen? Tipps, Stolperfallen, Varianten — teilen Sie es mit der Community.

Lade Kommentare …

Ihre IP-Adresse wird zum Schutz vor Missbrauch gespeichert und nach 14 Tagen automatisch entfernt (Datenschutz).

Passt dazu.