Marketplace

sf-deploy

Comprehensive Salesforce DevOps automation using sf CLI v2. Use when deploying metadata, managing scratch orgs, setting up CI/CD pipelines, or troubleshooting deployment errors.

$ インストール

git clone https://github.com/Jaganpro/sf-skills /tmp/sf-skills && cp -r /tmp/sf-skills/sf-deploy ~/.claude/skills/sf-skills

// tip: Run this command in your terminal to install the skill


name: sf-deploy description: > Comprehensive Salesforce DevOps automation using sf CLI v2. Use when deploying metadata, managing scratch orgs, setting up CI/CD pipelines, or troubleshooting deployment errors. license: MIT metadata: version: "2.0.0" author: "Jag Valaiyapathy"

sf-deploy: Comprehensive Salesforce DevOps Automation

Expert Salesforce DevOps engineer specializing in deployment automation, CI/CD pipelines, and metadata management using Salesforce CLI (sf v2).

Core Responsibilities

  1. Deployment Management: Execute, validate, and monitor deployments (metadata, Apex, LWC)
  2. DevOps Automation: CI/CD pipelines, automated testing, deployment workflows
  3. Org Management: Authentication, scratch orgs, environment management
  4. Quality Assurance: Tests, code coverage, pre-production validation
  5. Troubleshooting: Debug failures, analyze logs, provide solutions

⚠️ CRITICAL: Orchestration Order

┌─────────────────────────────────────────────────────────────────────────────┐
│  sf-metadata → sf-flow → sf-deploy → sf-data                               │
│                              ▲                                              │
│                         YOU ARE HERE                                        │
└─────────────────────────────────────────────────────────────────────────────┘

Deploy order WITHIN sf-deploy: Objects/Fields → Permission Sets → Apex → Flows (Draft) → Activate Flows

PhaseMetadata TypeWhy This Order
1Custom Objects/FieldsEverything references these
2Permission SetsFLS requires fields to exist
3Apex Classes@InvocableMethod needed before Flows
4Flows (as Draft)Flows reference fields and Apex
5Activate FlowsSafe to activate after validation

See docs/orchestration.md for detailed deployment workflows and agent deployment patterns.


🔑 Key Insights for Deployment

Always Use --dry-run First

# CORRECT: Validate before deploying
sf project deploy start --dry-run --source-dir force-app --target-org alias
sf project deploy start --source-dir force-app --target-org alias

# WRONG: Deploying without validation
sf project deploy start --source-dir force-app --target-org alias  # Risky!

Deploy Permission Sets After Objects

Common Error:

Error: In field: field - no CustomObject named ObjectName__c found

Solution: Deploy objects first, THEN permission sets referencing them.

Flow Activation (4-Step Process)

Flows deploy as Draft by default. Activation steps:

  1. Deploy with <status>Draft</status>
  2. Verify: sf project deploy report --job-id [id]
  3. Edit XML: DraftActive
  4. Redeploy

Why? Draft lets you verify before activating; if activation fails, flow still exists.

Common Errors: "Flow is invalid" (deploy objects first) | "Insufficient permissions" (check Manage Flow) | "Version conflict" (deactivate old version)

FLS Warning After Deployment

⚠️ Deployed fields may be INVISIBLE without FLS!

After deploying custom objects/fields:

  1. Deploy Permission Set granting field access
  2. Assign Permission Set to user: sf org assign permset --name PermSetName --target-org alias
  3. Verify field visibility

CLI Version (CRITICAL)

This skill uses sf CLI (v2.x), NOT legacy sfdx (v1.x)

Legacy sfdx (v1)Modern sf (v2)
--checkonly / --check-only--dry-run
sfdx force:source:deploysf project deploy start

Prerequisites

Before deployment, verify:

sf --version              # Requires v2.x
sf org list               # Check authenticated orgs
test -f sfdx-project.json # Valid SFDX project

Deployment Workflow (5-Phase)

Phase 1: Pre-Deployment Analysis

Gather via AskUserQuestion: Target org, deployment scope, validation requirements, rollback strategy.

Analyze:

  • Read sfdx-project.json for package directories
  • Glob for metadata: **/force-app/**/*.{cls,trigger,xml,js,html,css}
  • Grep for dependencies

TodoWrite tasks: Validate auth, Pre-tests, Deploy, Monitor, Post-tests, Verify

Phase 2: Pre-Deployment Validation

sf org display --target-org <alias>                    # Check connection
sf apex test run --test-level RunLocalTests --target-org <alias> --wait 10  # Local tests
sf project deploy start --dry-run --test-level RunLocalTests --target-org <alias> --wait 30  # Validate

Phase 3: Deployment Execution

Commands by scope:

# Full metadata
sf project deploy start --target-org <alias> --wait 30

# Specific components
sf project deploy start --source-dir force-app/main/default/classes --target-org <alias>

# Manifest-based
sf project deploy start --manifest manifest/package.xml --target-org <alias> --test-level RunLocalTests --wait 30

# Quick deploy (after validation)
sf project deploy quick --job-id <validation-job-id> --target-org <alias>

Handle failures: Parse errors, identify failed components, suggest fixes.

Phase 4: Post-Deployment Verification

sf project deploy report --job-id <job-id> --target-org <alias>

Verify components, run smoke tests, check coverage.

Phase 5: Documentation

Provide summary with: deployed components, test results, coverage metrics, next steps.

See examples/deployment-report-template.md for output format.

Deployment Variants: Production (full + RunAllTests), Hotfix (targeted + RunLocalTests), CI/CD (scripted + gates), Scratch (push source).

CLI Reference

Deploy: sf project deploy start [--dry-run] [--source-dir <path>] [--manifest <xml>] [--test-level <level>] Quick: sf project deploy quick --job-id <id> | Status: sf project deploy report Test: sf apex test run --test-level RunLocalTests | Coverage: sf apex get test --code-coverage Org: sf org list | sf org display | sf org create scratch | sf org open Metadata: sf project retrieve start | sf org list metadata --metadata-type <type>

Error Handling

ErrorCauseSolution
FIELD_CUSTOM_VALIDATION_EXCEPTIONValidation rule blockingDeactivate rules or use valid test data
INVALID_CROSS_REFERENCE_KEYMissing dependencyInclude dependencies in deploy
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITYTrigger/validation errorReview trigger logic, check recursion
TEST_FAILURETest class failureFix test or code under test
INSUFFICIENT_ACCESSPermission issueVerify user permissions, FLS

Flow-Specific Errors

ErrorCauseSolution
"Element X is duplicated"Elements not alphabetically orderedReorder Flow XML elements
"Element bulkSupport invalid"Deprecated element (API 60.0+)Remove <bulkSupport>
"Error parsing file"Malformed XMLValidate XML syntax

Failure Response

  1. Parse error output, identify failed components
  2. Explain error in plain language
  3. Suggest specific fixes with code examples
  4. Provide rollback options if needed

Best Practices

  • Always validate first: Use --dry-run for production
  • Appropriate test levels: RunLocalTests (deploy), RunAllTests (packages)
  • Code coverage: >75% for production, >90% recommended
  • Use manifests: package.xml for controlled deployments
  • Version control: Commit before deploying, tag releases
  • Incremental deploys: Small, frequent changes over large batches
  • Sandbox first: Always test before production
  • Backup metadata: Retrieve before major deployments
  • Quick deploy: Use for validated changesets

Trigger Deployment Safety

💡 See docs/trigger-deployment-safety.md for comprehensive guide.

Pre-Deployment Guardrails

Before deploying triggers, verify:

CheckCommand/Action
Trigger chain analysisMap all triggers firing together
Cascade failure reviewIdentify atomic vs independent processes
Async decouplingUse Queueable/Events for external calls
Savepoint usageVerify explicit atomicity where needed
Test coverageInclude cascade success/failure tests

Common Trigger Cascade Risks

RiskSymptomSolution
External callout in triggerCascade failure from HTTP timeoutMove to Queueable
Shared exception handlingOne failure rolls back allIsolate with try-catch or async
Recursive triggersStack overflow or DML errorsUse static flag recursion guard
Order-dependent triggersInconsistent behaviorDocument and test trigger order

Pre-Deployment Checklist

TRIGGER SAFETY CHECKLIST:
□ Identify all triggers in deployment
□ Map trigger chains (which triggers fire together)
□ Verify cascade behavior is intentional
□ Check for external callouts → should be async
□ Confirm savepoint usage for atomic operations
□ Test both success and failure cascade scenarios
□ Validate with --dry-run before production deploy

Recommended Async Patterns

// BAD: Synchronous external call in trigger
trigger AccountTrigger on Account (after insert) {
    ExternalService.sync(Trigger.new);  // Failure cascades
}

// GOOD: Async decoupling
trigger AccountTrigger on Account (after insert) {
    if (canEnqueueJob()) {
        System.enqueueJob(new AccountSyncQueueable(Trigger.newMap.keySet()));
    }
}

CI/CD Integration

Standard pipeline workflow:

  1. Authenticate (JWT/auth URL)
  2. Validate metadata
  3. Static analysis (PMD, ESLint)
  4. Dry-run deployment
  5. Run tests + coverage check
  6. Deploy if validation passes
  7. Notify

See examples/deployment-workflows.md for scripts.

Edge Cases

  • Large deployments: Split into batches (limit: 10,000 files / 39 MB)
  • Test timeout: Increase wait time or run tests separately
  • Namespace conflicts: Handle managed package issues
  • API version: Ensure source/target compatibility

Cross-Skill Dependency Checklist

Before deploying, verify these prerequisites from other skills:

DependencyCheck CommandRequired For
TAF Packagesf package installed list --target-org aliasTAF trigger pattern (sf-apex)
Custom Objects/Fieldssf sobject describe --sobject ObjectName --target-org aliasApex/Flow field references
Trigger_Action__mdtCheck Setup → Custom Metadata TypesTAF trigger execution
Queuessf data query --query "SELECT Id,Name FROM Group WHERE Type='Queue'"Flow queue assignments
Permission Setssf org list metadata --metadata-type PermissionSetFLS for custom fields

Common Cross-Skill Issues:

Error MessageMissing DependencySolution
Invalid type: MetadataTriggerHandlerTAF PackageInstall apex-trigger-actions package
Field does not exist: Field__cCustom Field or FLSDeploy field or create Permission Set
No such column 'Field__c'Field-Level SecurityAssign Permission Set to running user
SObject type 'Object__c' not supportedCustom ObjectDeploy object via sf-metadata first
Queue 'QueueName' not foundQueue MetadataDeploy queue via sf-metadata first

sf-ai-agentforce Integration (Agent DevOps)

Complete DevOps guide: See docs/agent-deployment-guide.md for comprehensive agent deployment documentation.

Agent Metadata Types

Metadata TypeDescription
BotTop-level chatbot definition
BotVersionVersion configuration
GenAiPlannerBundleReasoning engine (LLM config)
GenAiPluginTopic definition
GenAiFunctionAction definition

Agent Pseudo Metadata Type

The Agent pseudo type syncs all agent components at once:

# Retrieve agent + all dependencies from org
sf project retrieve start --metadata Agent:[AgentName] --target-org [alias]

# Deploy agent metadata to org
sf project deploy start --metadata Agent:[AgentName] --target-org [alias]

Agent Lifecycle Commands

# Activate agent (makes available to users)
sf agent activate --api-name [AgentName] --target-org [alias]

# Deactivate agent (REQUIRED before making changes)
sf agent deactivate --api-name [AgentName] --target-org [alias]

# Preview agent (simulated mode - safe testing)
sf agent preview --api-name [AgentName] --target-org [alias]

# Preview agent (live mode - real Apex/Flows)
sf agent preview --api-name [AgentName] --use-live-actions --client-app [App] --target-org [alias]

# Validate Agent Script syntax
sf afdx agent validate --api-name [AgentName] --target-org [alias]

Full Agent Deployment Workflow

# 1. Deploy Apex classes (if any)
sf project deploy start --metadata ApexClass --target-org [alias]

# 2. Deploy Flows
sf project deploy start --metadata Flow --target-org [alias]

# 3. Validate Agent Script
sf afdx agent validate --api-name [AgentName] --target-org [alias]

# 4. Publish agent
sf agent publish --api-name [AgentName] --target-org [alias]

# 5. Preview (simulated mode)
sf agent preview --api-name [AgentName] --target-org [alias]

# 6. Activate
sf agent activate --api-name [AgentName] --target-org [alias]

Modifying Existing Agents

⚠️ Deactivation Required: You MUST deactivate an agent before modifying topics, actions, or system instructions.

# 1. Deactivate
sf agent deactivate --api-name [AgentName] --target-org [alias]

# 2. Make changes to Agent Script

# 3. Re-publish
sf agent publish --api-name [AgentName] --target-org [alias]

# 4. Re-activate
sf agent activate --api-name [AgentName] --target-org [alias]

Sync Agent Between Orgs

# 1. Retrieve from source org
sf project retrieve start --metadata Agent:[AgentName] --target-org source-org

# 2. Deploy dependencies to target org first
sf project deploy start --metadata ApexClass,Flow --target-org target-org

# 3. Deploy agent metadata
sf project deploy start --metadata Agent:[AgentName] --target-org target-org

# 4. Publish agent in target org
sf agent publish --api-name [AgentName] --target-org target-org

# 5. Activate in target org
sf agent activate --api-name [AgentName] --target-org target-org

Agent-Specific CLI Reference

CommandDescription
sf agent publish --api-name XPublish authoring bundle
sf agent activate --api-name XActivate published agent
sf agent deactivate --api-name XDeactivate agent for changes
sf agent preview --api-name XPreview agent behavior
sf afdx agent validate --api-name XValidate Agent Script syntax
sf org open agent --api-name XOpen in Agentforce Builder
sf project retrieve start --metadata Agent:XRetrieve agent + components
sf project deploy start --metadata Agent:XDeploy agent metadata

Deployment Script Template

Reusable multi-step deployment script: examples/deploy.sh

Deploys in order: Objects → Permission Sets → Apex (with tests) → Flows (Draft)


Generate Package Manifest

Auto-generate package.xml from source directory:

# Generate from source
sf project generate manifest --source-dir force-app --name manifest/package.xml

# Generate for specific metadata types
sf project generate manifest \
    --metadata CustomObject:Account \
    --metadata ApexClass \
    --metadata Flow \
    --name manifest/package.xml

# Deploy using manifest
sf project deploy start --manifest manifest/package.xml --target-org alias

When to use manifest vs source-dir:

ScenarioUseCommand
Deploy everything--source-dirsf project deploy start --source-dir force-app
Deploy specific components--manifestsf project deploy start --manifest package.xml
CI/CD pipelines--manifestControlled, reproducible deployments
Development iteration--source-dirQuick local changes

Notes

  • CLI: Uses only sf (v2) with modern flag syntax
  • Auth: Supports OAuth, JWT, Auth URL, web login
  • API: Uses Metadata API (not Tooling API)
  • Async: Use --wait to monitor; most deploys are async
  • Limits: Be aware of Salesforce governor limits

License

MIT License. See LICENSE file. Copyright (c) 2024-2025 Jag Valaiyapathy