--- name: platform-sharing-rules-generate description: "Use this skill when users need to create, generate, or modify Salesforce Sharing Rules metadata. TRIGGER when: users mention sharing rules, record sharing, criteria-based sharing, role-based sharing, guest user sharing, portal user sharing, sharingRules, sharingCriteriaRules, sharingGuestRules, sharingOwnerRules, .sharingRules-meta.xml files, or ask to share records with specific roles or groups. Also trigger when users want to configure record-level access beyond org-wide defaults (OWD), share object records with roles, groups, or guest users, or set up Experience Site guest user record visibility. SKIP when: user needs permission sets or profiles (use platform-permission-set-generate), or needs object-level security rather than record-level sharing (use platform-permission-set-generate)." metadata: version: "1.0" --- # Sharing Rules Generator Generate Salesforce Sharing Rules metadata to control record-level access beyond org-wide defaults. Supports criteria-based rules, role/group-based owner rules, and guest user rules for Experience Sites. ## Scope - **In scope**: Generating `sharingCriteriaRules`, `sharingOwnerRules`, and `sharingGuestRules` metadata; retrieving existing sharing rules from an org; appending new rules to existing files; configuring rules for Guest and Portal profiles. - **Out of scope**: Changing org-wide defaults (OWD/sharing model), creating Experience Sites, configuring permission sets or profiles (use `platform-permission-set-generate`), territory-based sharing rules. --- ## Clarifying Questions Before generating, confirm with the user if not already clear: - Which object should the sharing rule apply to? (standard or custom object API name) - What type of rule? (criteria-based, role/group-based owner rule, or guest user rule) - Who should records be shared with? (role name, group, portal role, or guest user nickname) - What access level? (Read or Read/Write) - For criteria-based rules: what field conditions should match? --- ## Required Inputs Gather or infer before proceeding: - **Object API name**: The sObject the rule targets (e.g., `Account`, `Property__c`) - **Rule type**: One of `sharingCriteriaRules`, `sharingOwnerRules`, or `sharingGuestRules` - **Shared-to target**: Role, group, portal role, or guest user community nickname - **Access level**: `Read` or `Edit` (maps to Read-Only or Read/Write) - **Criteria** (for criteria/guest rules): Field name, operation, and value for each filter item Defaults unless specified: - Access level: `Read` - `includeRecordsOwnedByAll`: `true` for criteria rules - `includeHVUOwnedRecords`: `false` for guest rules - Account sharing rules include `accountSettings` with all sub-access levels set to `None` --- ## Workflow All steps are sequential. Do not skip or reorder. ### Phase 1 — Discover 1. **Resolve the SFDX project path** — find the project's `sfdx-project.json` and identify the package directory for `sharingRules/`. 2. **Check for existing sharing rules** — look for `/sharingRules/.sharingRules-meta.xml`. If found, read it to understand existing rules and avoid duplicates. 3. **If no local file exists**, retrieve from the org: ```sh sf project retrieve start --metadata "SharingRules:" --target-org ``` ### Phase 2 — Determine Rule Type 4. **Select the rule type** based on user intent. Read `references/rule-types.md` for the complete schema of each type and its required elements. 5. **For Account sharing rules**: the `accountSettings` element is required. Default sub-access levels to `None` unless the user specifies otherwise. 6. **For Guest rules**: the `sharedTo` must use `` with the site guest user's community nickname. Never use `` or `` for guest rules. ### Phase 3 — Generate 7. **Construct the XML** following the schema in `references/rule-types.md`. Key structure: - One `.sharingRules-meta.xml` file per object - All rules for the same object go in the same file - If appending to an existing file, add the new rule element inside the existing `` root 8. **Name the rule** — derive `` from the intent (PascalCase, no spaces, descriptive). Generate a matching `