Build Salesforce Agentforce using Agent Script in Visual Studio Code

Build Salesforce Agentforce using Agent Script in Visual Studio Code

Build Salesforce Agentforce Using Agent Script in Visual Studio Code: A Developer’s Guide

The era of AI-driven automation in Salesforce is here. For Salesforce Developers, Architects, and Admins, Salesforce Agentforce represents a massive leap forward in building intelligent, autonomous agents that can execute complex workflows.

While declarative tools are great, developers love to develop using an IDE like Visual Studio Code (VS Code). Working locally in an IDE allows for robust version control, seamless integrations, and a faster coding experience using Agent Script.

In this technical guide, we will walk you through how to extract, create, validate, publish, and preview Salesforce Agentforce agents directly within Visual Studio Code.


Why Build Agentforce Agents in VS Code?

For technical personas—Software Engineers, Developers, and Analysts—moving Agentforce development into VS Code offers several distinct advantages:

  • Familiar Tooling: Leverage the powerful extensions, linting, and shortcuts you already use daily.
  • Source-Driven Development: Easily manage your AI agent configurations alongside your Apex classes and LWC components in Git.
  • Speed: Use the Command Palette (AFDX commands) to rapidly scaffold, test, and deploy agents without clicking through UI screens.

Step 1: Extract Existing Agents to Your Local Machine

Before building a new agent, you may want to pull down existing ones from your Salesforce Org to modify or study them. You can extract existing agents developed in your org using two primary methods:

Method A: The Org Browser

The Org Browser in Visual Studio Code is the fastest way to view and retrieve metadata from your default org without writing a manifest.

Simply navigate to the Org Browser cloud icon in your VS Code sidebar, locate the Agent metadata type, and click the download icon to retrieve it to your local project.

Method B: Using Package.xml

For a more structured, source-trackable approach, you can define your extraction criteria in your package.xml manifest file. Use the following snippet to retrieve your AI Authoring Bundles:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>*</members>
        <name>AiAuthoringBundle</name>
    </types>
    <version>66.0</version>
</Package>

Run your standard SFDX retrieve command against this manifest to pull the agents into your local directory.


Step 2: Create a New Agentforce Agent

Once your environment is set up and authenticated to your Salesforce Org, creating a new agent is just a command away.

Open your VS Code Command Palette (Ctrl+Shift+P on Windows/Linux or Cmd+Shift+P on macOS) and use the following command to create an Agent:

AFDX: Create Agent

This will scaffold the necessary files and Agent Script templates required to define your agent’s instructions, capabilities, and data access.


Step 3: Validate Your Agent Script

Errors in your Agent Script can lead to unpredictable AI behavior or deployment failures. Fortunately, the AFDX tooling provides local validation.

Once you have written or modified your agent’s script, you need to ensure it meets Salesforce’s structural and logical requirements.

  1. Open your Agent file in the Visual Studio Code editor.
  2. Right-click anywhere within the editor window.
  3. Choose “AFDX: Validate This Agent”.

This command will parse your script and highlight any syntax errors or missing required parameters right in your IDE.


Step 4: Preview and Publish Your Agent

Testing and deploying your agent is seamless with AFDX commands.

Preview the Agent

Before pushing your agent live to production or a testing environment, you can preview how it interprets instructions and interacts with data. Open the Command Palette and run:

AFDX: Preview this Agent

Publish the Agent

Once you are satisfied with your local testing and validation, it is time to deploy your agent to your Salesforce org. Open the Command Palette and execute:

AFDX: Publish this Agent

Your agent is now live and ready to be integrated into your Salesforce workflows, Copilot surfaces, or automated backend processes!


💡 Best Practices & Recommendations

While the steps above cover the mechanics of building agents, following these technical best practices will ensure your Agentforce implementations are scalable and maintainable:

  • Version Control Everything: Always treat your AiAuthoringBundle metadata just like Apex or LWC. Commit your Agent Scripts to your Git repository to track changes, enable peer reviews, and facilitate CI/CD pipelines.
  • Keep API Versions Updated: In the provided package.xml snippet, the API version is set to 66.0 (Spring ’26). Always ensure your package.xml and sfdx-project.json files are aligned with the latest Salesforce release to take advantage of the newest Agentforce features.
  • Modular Agent Design: Avoid creating monolithic agents. Instead of one massive agent trying to do everything, build smaller, specialized agents with clear scopes (e.g., a “Customer Service Agent” vs. a “Quote Generation Agent”). This makes validation and debugging much easier.
  • Pre-Publish Testing: Always run AFDX: Validate This Agent before attempting to publish. This saves deployment time by catching syntax and structural errors locally rather than waiting for an org deployment failure.
  • Security Context: Remember that agents execute actions based on the user’s context. Always test your previewed agents with different user profiles to ensure field-level security (FLS) and sharing rules are respected by the AI.

Salesforce Article:

https://developer.salesforce.com/docs/ai/agentforce/guide/agent-dx-nga-preview.html

https://developer.salesforce.com/docs/ai/agentforce/guide/agent-dx-nga-author-agent.html

Leave a Reply