Skip to content

forcedotcom/sf-skills / plugins/builder/salesforce-code-quality/skills/dx-code-analyzer-custom-rule-create

dx-code-analyzer-custom-rule-create

Verified

Create custom Code Analyzer rules for Regex (pattern matching), PMD (XPath/AST for Apex and metadata XML), and ESLint (LWC/JavaScript/TypeScript). Use when users want to enforce coding standards, ban patterns, detect hardcoded values, govern metadata, or add rules not in the built-in set. TRIGGER when: user says 'create a rule', 'ban System.debug', 'enforce naming convention', 'detect hardcoded IDs', 'custom rule', 'xpath rule', 'regex rule', 'add a PMD rule', 'enforce a policy', 'create a check for', 'flag this pattern', 'make a rule that catches', 'metadata rule', 'check permissions', 'enforce API version', 'eslint rule', 'lwc rule', 'override rule threshold', 'customize complexity', or describes a pattern to enforce. DO NOT TRIGGER when: user wants to run a scan (use dx-code-analyzer-run), configure engines (use dx-code-analyzer-configure), or explain existing rules (use dx-code-analyzer-run).

examples/metadata-xml-example-flows.md

View on GitHub
# Metadata XML Examples: Flows

[← Back to Metadata XML Examples Index](metadata-xml-examples.md)

## Example 5: Require Flow Auto-Layout

**Problem:** Flows should use Auto-Layout canvas for consistency and maintainability.

**Target file:** `*.flow-meta.xml`

**Sample violating metadata:**
```xml
<Flow xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>58.0</apiVersion>
    <label>My Flow</label>
    <processMetadataValues>
        <name>CanvasMode</name>
        <value><stringValue>FREE_FORM_CANVAS</stringValue></value>
    </processMetadataValues>
</Flow>
```

**XPath (PMD 7 — flags FREE_FORM_CANVAS):**
```xpath
//*[@Text='FREE_FORM_CANVAS']/../..
  [local-name()='value']
  [ancestor::*[local-name()='processMetadataValues'][.//*[@Text='CanvasMode']]]
```

**How it works:** Finds text nodes with "FREE_FORM_CANVAS", navigates up to the `<value>` element, then confirms it's inside a `processMetadataValues` block that also contains "CanvasMode".

**PMD ruleset:**
```xml
<rule name="FlowMustUseAutoLayout"
      language="xml"
      message="Flows must use AUTO_LAYOUT_CANVAS mode"
      class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
    <description>Enforces auto-layout canvas mode on all flows for consistency</description>
    <priority>3</priority>
    <properties>
        <property name="xpath">
            <value><![CDATA[
//*[@Text='FREE_FORM_CANVAS']/../..
  [local-name()='value']
  [ancestor::*[local-name()='processMetadataValues'][.//*[@Text='CanvasMode']]]
]]></value>
        </property>
    </properties>
</rule>
```

---

## Example 6: Flow Missing Fault Handler

**Problem:** Flow actions without fault connectors can fail silently.

**Target file:** `*.flow-meta.xml`

**Sample violating metadata:**
```xml
<Flow xmlns="http://soap.sforce.com/2006/04/metadata">
    <recordCreates>
        <name>Create_Account</name>
        <object>Account</object>
        <connector>
            <targetReference>Next_Step</targetReference>
        </connector>
        <!-- No <faultConnector> element! -->
    </recordCreates>
</Flow>
```

**XPath:**
```xpath
//*[local-name()='Flow']/*[
  local-name()='recordCreates' or local-name()='recordUpdates'
  or local-name()='recordDeletes' or local-name()='recordLookups'
][
  not(*[local-name()='faultConnector'])
]
```

**PMD ruleset:**
```xml
<rule name="FlowActionRequiresFaultHandler"
      language="xml"
      message="Flow DML/query actions must have a fault connector for error handling"
      class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
    <description>Ensures all data operations in flows have fault paths</description>
    <priority>2</priority>
    <properties>
        <property name="xpath">
            <value><![CDATA[
//*[local-name()='Flow']/*[
  local-name()='recordCreates' or local-name()='recordUpdates'
  or local-name()='recordDeletes' or local-name()='recordLookups'
][
  not(*[local-name()='faultConnector'])
]
]]></value>
        </property>
    </properties>
</rule>
```
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 321kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 321kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 321kUpdated Claude CodeCodexVerifiedInstall
skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.Stars: 197kUpdated Claude CodeCodexVerifiedInstall