Design Managed Apex for Agentforce
As an independent software vendor (ISV) developer, you can build custom agent actions using Apex and distribute them in managed packages. To ensure that subscriber admins can declaratively configure your Apex agent actions and that Agentforce can invoke the actions at run time, follow these requirements and recommendations.
For Agentforce to use Apex agent actions in managed packages, these Apex members must have the global access modifier.
- The Apex class containing the
@InvocableMethodthat defines the agent action. - The input wrapper class that defines the parameters an admin can configure for the action, and the output wrapper class that defines the structured result returned to Agentforce.
- All
@InvocableVariablemembers within these input and output wrapper classes. - Any custom Apex data types used as properties in your wrapper classes.
If any of these Apex members aren’t global, then the Apex agent action can’t be invoked by Agentforce at run time.
These Apex members must be global because Agentforce agents currently can’t be packaged directly, and therefore can’t have a namespace. By definition, this means that Apex agent actions don’t have access to non-global Apex, such as public Apex, that’s part of a managed package and does have a namespace.
Importantly, managed global Apex is subject to stricter manageability rules than managed non-global Apex. See the Global Apex Manageability Rules section of Best Practices for Using Global Apex in Managed Packages.
Although global Apex is required for any direct entry point to an agent action, delegate any business logic or heavy lifting to public classes and methods. See the Delegate from Thin Global Entry Points section of Best Practices for Using Global Apex in Managed Packages.
To define an Apex agent action, use the @InvocableMethod annotation and follow these requirements.
-
Your Apex method must be
global static. -
Your Apex method must be annotated with
@InvocableMethod (label='Your Action Name' description='Clear, concise description of what the action does' category='Your ISV App Name').- Use clear and descriptive
labelanddescriptionmodifiers. The Agentforce reasoning engine uses them to determine when to invoke the action. Subscriber admins configuring Agentforce also use them to help decide which agent topics to add the action to. - Use the
categorymodifier to help organize actions. We recommend using your ISV app name.
- Use clear and descriptive
Only one method in a class can have the @InvocableMethod annotation. Create a separate global Apex class for each agent action in your managed package.
In addition to the requirements of using global Apex and the @InvocableMethod annotation, we also recommend using custom global classes to structure input and output parameters. By using parameter objects, you avoid changing the global method signature when you modify the parameters of the agent action. To learn how to use this pattern, see the Use Parameter Objects for Global Method Inputs and Return Types section of Best Practices for Using Global Apex in Managed Packages. Then review these targeted guidelines to implement Apex agent actions using this pattern.
Because you can’t change managed global method signatures, make signatures flexible.
-
Define
globalinner Apex classes to serve as containers for input and output parameters. These classes can be in the same top-level class as the invocable method. -
Annotate both input and output classes with
@JsonAccess(serializable='always' deserializable='always'). The @JsonAccess annotation governs the serialization and deserialization of managed Apex. Because Agentforce serializes and deserializes complex Apex types from an unmanaged context at run time, both@JsonAccessparameters must be set to'always'. -
Within these input and output classes, declare
globalmember variables annotated with@InvocableVariable(label='User-Friendly Name' description='Description of this parameter' required=true/false).- Use clear and descriptive
labelanddescriptionmodifiers, so that subscribers can configure the inputs declaratively, and Agentforce can understand the output. - Set the
requiredmodifier astrueorfalseto specify whether the input is required for the agent action to run. This modifier also helps subscriber admins configure your actions.
- Use clear and descriptive
-
Define the invocable method to accept a
Listof its input class type, for exampleList<MyInputAction> requests. -
Define the invocable method to return a
Listof its output class type, for exampleList<MyOutputAction> results.
In this example, the getCoordinates method is defined as an @InvocableMethod so it can be invoked by Agentforce. The method accepts a list of GeocodingRequest objects and returns a corresponding list of GeocodingResponse objects. The input and output wrapper classes are both annotated with @JsonAccess(serializable='always' deserializable='always') so Agentforce can serialize and deserialize the objects from an unmanaged context. The properties of both wrapper classes are defined as @InvocableVariable so an admin can configure them declaratively. The label and description modifiers in both @InvocableMethod and @InvocableVariable are important because they help the Agentforce reasoning engine to understand how to use the action.
See Also
- Best Practices for Using Global Apex in Managed Packages
- Access Modifiers
- InvocableMethod Annotation
- InvocableVariable Annotation
- Use the with sharing, without sharing, and inherited sharing Keywords
- Set an Access Mode for Database Operations
- Salesforce Developers Blog: Build Custom Agent Actions Using Apex