Sub topic 1
Building Agentforce Actions with Invocable Apex
Prerequisites
Assume you know basic Apex classes, collections, and SOQL. You should also understand Salesforce objects, sharing, and the role of topics in an Agentforce agent.
What Agentforce actions are and when to use Apex
An Agentforce action is an operation an agent can choose during a conversation. The action receives structured inputs, performs work, and returns structured outputs. Actions can use flows, prompt templates, standard Salesforce operations, or custom Apex.
Use Apex when the operation needs custom logic, several data-processing steps, or an integration that declarative tools cannot express clearly. Keep an action focused. For example, “Get open cases for an account” is a better action than “Manage every account task.”
Writing an `@InvocableMethod`
@InvocableMethod exposes one public static method to automation tools and Agentforce. The method accepts a list of requests and returns a list of responses. The list-based contract matters even when one conversation currently supplies one request.
public with sharing class GetAccountNameAction {
public class Request {
@InvocableVariable(
label='Account Id'
description='The Salesforce Id of the account'
required=true
)
public Id accountId;
}
public class Response {
@InvocableVariable(
label='Account Name'
description='The name of the account'
)
public String accountName;
}
@InvocableMethod(
label='Get Account Name'
description='Returns the name for a Salesforce account'
category='Accounts'
)
public static List<Response> getAccountName(List<Request> requests) {
List<Response> results = new List<Response>();
for (Request request : requests) {
Response result = new Response();
result.accountName = 'Account: ' + request.accountId;
results.add(result);
}
return results;
}
}Modeling inputs and outputs with `@InvocableVariable`
A request and response wrapper is a class that groups the action’s fields. Mark each agent-visible field with @InvocableVariable. Use types that match the data the agent must provide or explain, such as Id, String, Boolean, and Integer.
Writing labels and descriptions the agent can use
Labels appear in the action configuration. Descriptions help the reasoning engine select the action and fill its inputs. Name the business meaning, state whether an identifier is required, and describe the output’s format. Avoid vague text such as “value” or “process account.”
Exercise
Why does an invocable method accept a list of requests instead of one request?
Show answer
The platform uses a collection-based contract so the method can process multiple inputs safely and consistently.