Introduction
Einstein Copilot serves as a conversational AI assistant tailored for Salesforce users. Leveraging your organization’s metadata and data permissions, it executes commands in natural language. While equipped with a set of standard actions within the bounds of the Einstein Trust Layer, what distinguishes Einstein Copilot is its adaptability, facilitated by Copilot Builder. Through Copilot Builder, developers can amplify Copilot’s functions by crafting custom actions using Apex, flows, or prompt templates. This exceptional flexibility enables Salesforce Developers to personalize Einstein Copilot to align with their specific business requirements, resulting in unique and tailored conversational AI assistants.
Craft a straightforward custom action utilizing Apex:
public with sharing class SayHello {
@InvocableMethod(label=’Say Hello’ description=’Say Hello to the user’)
public static List<String> sayHello(List<String> name) {
if (!String.isBlank(name[0])) {
return new List<String>{ ‘Hello ‘ + name[0] };
} else {
return new List<String>{ ‘Error saying Hello. Input is missing’ };
}
}
}
Please note that I’ve included a method annotated with @InvocableMethod in our Apex class, which enables the action to be accessed and configured within Einstein Copilot Studio.
Utilize @InvocableMethod to label and describe your Apex method, and @InvocableVariable to define input variables. These annotations enable Copilot to effectively match user conversation intents with the appropriate actions by leveraging these descriptions.
Apex actions in Copilot Builder
In Copilot Builder, we can set up the Apex action you previously created as a Copilot action. This integration allows the action to be included in Copilot’s library of actions.
Refer to the screenshot below to add instructions for your action, define inputs, and specify outputs. This step is crucial as Einstein Copilot utilizes these descriptions to accurately match user conversation intents with the appropriate actions. You may need to refine these descriptions to ensure your actions work effectively with different utterances.
Test custom actions with Copilot Builder
To begin testing your action, add it to your Copilot using Copilot Builder. Once added, the action will appear in the “This Copilot’s Actions” tab. Refer to the screenshot below to see how you can include your custom action from the Copilot Action Library tab. For optimal performance, it’s advisable to assign no more than 15 actions to a Copilot. Further details on considerations and limits can be found in the documentation.
Utilize Copilot Builder to test and troubleshoot the planner service’s capability to invoke the custom action. For further debugging, activate enhanced event logs. You can access and review the event logs directly from Copilot Builder.
Best practices for writing Apex code tailored for Einstein Copilot
Here are some recommended practices to adhere to when writing Apex code for Einstein Copilot:
- Ensure clear and descriptive labeling and descriptions for Invocable Apex classes and variables. Refer to relevant documentation for guidance on providing detailed instructions.
- Adhere to Apex security best practices, including implementing sharing mode and executing in user mode.
- Employ bulkified Apex code to mitigate risks of exceeding governor limits.
- Prioritize speed by avoiding intricate and time-intensive processes.
- Optimize SOQL queries, particularly when dealing with objects containing large volumes of data (LDV objects).
- Conduct thorough testing of Apex code in isolated environments, employing both unit and integration tests for comprehensive coverage.
Conclusion
Custom actions within Einstein Copilot enable developers to construct conversational AI assistant applications seamlessly, eliminating the necessity for multiple tools to orchestrate agents. With internal management of prompt handling techniques and LLM APIs, Copilot custom actions streamline the process, allowing developers to concentrate on crafting the AI assistant experience tailored to the business use case.