Agentforce & Einstein with Apex
Agentforce (launched 2024) lets you build autonomous AI agents on Salesforce.
40.1 Overview
Agentforce (launched 2024) lets you build autonomous AI agents on Salesforce. Agents reason over topics and call actions. Actions can be standard, Flows, prompt templates, or Apex invocable methods — so well-written invocable Apex becomes a tool the agent can use.
40.2 An Apex agent action
Guidelines for agent actions:
Write clear labels and descriptions for the method and every variable — the reasoning engine uses them to decide when and how to call the action.
Enforce security with user mode; the agent runs as a specific user (e.g., the agent user or the end user, depending on agent type).
Return concise, structured outputs; avoid huge payloads.
Keep actions idempotent and bulk-safe; handle "not found" gracefully instead of throwing.
Test actions with normal Apex tests and in Agentforce Builder/Testing Center.
public with sharing class OrderStatusAction {
public class Request {
@InvocableVariable(label='Order Number' description='The customer-facing order number, e.g., 00012345' required=true)
public String orderNumber;
}
public class Response {
@InvocableVariable(label='Status' description='Current fulfillment status of the order')
public String status;
@InvocableVariable(label='Expected Delivery' description='Estimated delivery date, if shipped')
public Date expectedDelivery;
}
@InvocableMethod(label='Get Order Status'
description='Looks up the fulfillment status and expected delivery date of an order by its order number.')
public static List<Response> getStatus(List<Request> requests) {
Set<String> numbers = new Set<String>();
for (Request r : requests) numbers.add(r.orderNumber);
Map<String, Order> byNumber = new Map<String, Order>();
for (Order o : [SELECT OrderNumber, Status, Expected_Delivery__c FROM Order
WHERE OrderNumber IN :numbers WITH USER_MODE]) {
byNumber.put(o.OrderNumber, o);
}
List<Response> out
…40.3 Other AI-related Apex touchpoints
Prompt templates can be grounded with Apex: an invocable method can supply data to a prompt template (configured with a capabilityType on @InvocableMethod; see the Prompt Builder documentation for the exact template-type strings).
Invoking prompt templates from Apex via ConnectApi.EinsteinLLM.generateMessagesForPromptTemplate(...).
Models API (aiplatform namespace) for calling LLMs through the Einstein Trust Layer.
Agentforce APIs and features evolve quickly — always check the current release notes and developer guide before building on them.