Sub topic 2
Handling a List of Requests in One Pass
Assume you know Apex classes, collections, SOQL, and Salesforce sharing concepts. An invocable action receives a list because Agentforce may process several requests in one transaction. Write the method to handle the entire list, even when the agent usually sends one request.
Bulkify the action
Collect all input IDs first, query once, and group the records in a map. Then build one response for each request in the original order. Do not place SOQL or DML inside the request loop.
public with sharing class GetOpenCasesAction {
public class Request {
@InvocableVariable(required=true)
public Id accountId;
}
public class Response {
@InvocableVariable
public Integer caseCount;
@InvocableVariable
public String message;
}
@InvocableMethod(label='Get Open Cases')
public static List<Response> run(List<Request> requests) {
Set<Id> accountIds = new Set<Id>();
for (Request request : requests) {
if (request.accountId != null) {
accountIds.add(request.accountId);
}
}
Map<Id, Integer> countsByAccount = new Map<Id, Integer>();
for (AggregateResult row : [
SELECT AccountId accountId, COUNT(Id) caseCount
FROM Case
WHERE AccountId IN :accountIds AND IsClosed = false
WITH USER_MODE
GROUP BY AccountId
]) {
countsByAccount.put(
(Id) row.get('accountId'),
(Integer) row.get('caseCount')
);
}
List<Response> responses = new List<Response>();
for (Request request : requests) {
Response response = new Response();
response.caseCount = countsByAccount.containsKey(request.accountId)
? countsByAccount.get(request.accountId) : 0;
response.message = response.caseCount == 0
? 'No open cases were found.'
: 'Open cases found: ' + response.caseCount;
responses.add(response);
}
return responses;
}
}The response list must match the request list by position. A map makes lookup efficient without changing that order.
Respect security
with sharing applies record-level sharing rules. WITH USER_MODE applies the running user's object and field permissions to the query. Use both when the action should behave like the current user. Avoid returning fields that the user cannot access.
> callout warning > Security is part of the action contract. An action that bypasses access rules can expose data through an agent even when the user cannot view it directly.
Return explainable results
Return a clear message for valid empty results. For invalid input or recoverable business failures, return a per-request status and explanation when the response type supports them. Reserve thrown exceptions for failures the agent cannot reasonably handle. Never return raw stack traces or sensitive data.
Account for limits and callouts
Bulkification protects SOQL and CPU limits, but every request still shares one transaction's governor limits. Bound result sizes and avoid unnecessary fields. If the action calls an external service, use the invocable method's callout support, handle timeouts, and return a user-safe failure message. Do not make one callout per request when the service supports batching.
Exercise
Why should a method query before it loops over requests?
Show answer
One shared query avoids per-request SOQL and reduces the risk of hitting governor limits.