How to Automate Salesforce Apex Execution and Deployment in VS Code or Cursor IDE?
As Salesforce developers, architects, and engineers, maximizing efficiency is always a top priority. Moving between the terminal, executing Anonymous Apex scripts, and running deployment commands can break your flow state.
What if you could trigger your entire setup—deploying your code and executing your initial data loads—with a single keyboard shortcut?
By leveraging the native tasks.json configuration in VS Code or Cursor, you can automate your Salesforce CLI (sf) commands into seamless, reproducible workflows.
Why Automate with VS Code and Cursor Tasks?
For technical admins, analysts, and developers, automation isn’t just about saving a few seconds; it’s about reducing human error.
- Consistency: Ensure scripts run in the exact order required every single time.
- Speed: No need to re-type long Salesforce CLI commands or search your terminal history.
- Onboarding: Share your
.vscode/tasks.jsonfile with your team so everyone uses the same deployment standards instantly.
Setting Up Your Automated Workflow
Both VS Code and Cursor allow you to define custom tasks inside a .vscode/tasks.json file at the root of your project directory.
Below is a complete, production-ready configuration that automates source deployment, executes an Anonymous Apex data load script, and chains them together.
The tasks.json Configuration
{
"version": "2.0.0",
"tasks": [
{
"label": "Deploy to Org",
"type": "shell",
"command": "sf project deploy start --source-dir force-app/main/default",
"group": "build",
"presentation": {
"reveal": "always",
"panel": "shared"
",
"problemMatcher": []
},
{
"label": "Run Apex: initialDataLoad",
"type": "shell",
"command": "sf apex run --file scripts/apex/initialDataLoad.apex",
"presentation": {
"reveal": "always",
"panel": "shared"
},
"problemMatcher": []
},
{
"label": "Setup: Run Apex + Deploy to Org",
"dependsOrder": "parallel",
"dependsOn": ["Run Apex: initialDataLoad", "Deploy to Org"],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "always",
"panel": "shared"
},
"problemMatcher": []
}
]
}
How to Run Your New Automation
Once you save this file in your .vscode/ directory, triggering these tasks is simple:
- Open the Command Palette (Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on macOS).
- Type and select Tasks: Run Task.
- Select your desired task (e.g.,
Setup: Run Apex + Deploy to Org).
Because the compound task is set as the default build group ("isDefault": true), you can also trigger it instantly using your IDE’s default build shortcut (Ctrl+Shift+B or Cmd+Shift+B).
Code Review: Best Practices & Recommendations
While the shared configuration is highly effective for quick tasks, architects and senior developers should consider a few key adjustments when scaling this configuration across a team or CI/CD pipelines:
1. Re-evaluate Dependency Order (dependsOrder)
In the compound task Setup: Run Apex + Deploy to Org, the configuration uses "dependsOrder": "parallel".
- The Risk: Running a source deployment at the exact same time as an Anonymous Apex execution can cause race conditions or deployment failures if the Apex script relies on metadata being deployed simultaneously.
- The Recommendation: Change
"dependsOrder": "parallel"to"dependsOrder": "sequence"if your data load script depends on the freshly deployed components.
2. Path Flexibility
The Run Apex task targets a hardcoded script path (scripts/apex/initialDataLoad.apex).
- The Recommendation: For general-purpose tasks, you can leverage VS Code inputs (e.g.,
${relativeFile}) so the task dynamically runs whichever Apex file you currently have open in your editor.
3. Handle Scratch Orgs and Target Flags
The commands rely on your active default org (-o flag omitted).
- The Recommendation: If you manage multiple environments (Scratch Orgs, Sandboxes, Production), ensure your active org is correctly set via
sf org default setbefore running the tasks, or append--target-orgvariables to your commands to avoid accidental deployments.