Can Apex return tables in Agentforce?
Question:
I am trying to return a formatted table of results in Agentforce using an Apex @InvocableMethod
. My scratch org has the features Einstein1AIPlatform
and enableEinsteinGptPlatform
enabled. The following Apex code is invoked successfully, with debug logs confirming the method completes and returns as expected:
@InvocableMethod(
label='List documents related to records'
description='List documents related to the named Fund including those related to its child records.'
)
public static Response[] listDocuments(Request[] requests) {
// Method logic here
}
The returned inner classes are structured as follows:
global inherited sharing class Response {
@InvocableVariable(label='Documents')
public Document[] documents = new Document[] {};
}
global inherited sharing class Document {
@InvocableVariable(label='Content Version Id')
public Id contentVersionId;
@InvocableVariable(label='Title')
public String title;
@InvocableVariable(label='Size')
public Integer size;
@InvocableVariable(label='Created Date')
public Datetime createdDate;
}
When invoked, the returned JSON response looks like this:
[{
"documents": [{
"title": "Agenda",
"size": 828,
"createdDate": "2024-12-20T13:05:47.000Z",
"contentVersionId": "068Oy000009lyraIAA"
}, {
"title": "Sales",
"size": 324296,
"createdDate": "2024-12-19T11:48:03.000Z",
"contentVersionId": "068Oy000009kOS0IAM"
}]
}]
Despite these efforts, I encounter an error in the Agentforce Event Log: “We couldn’t retrieve the action output,” with a null error message. The Apex class is included in the Einstein User profile. Additionally, I’ve attempted different instructions, such as asking Agentforce to format the Documents
variable as rows in a table, but the error persists.
Does Agentforce support returning and formatting tables from an Apex @InvocableMethod
? If so, what patterns or techniques should I use to ensure the table is displayed correctly?
Answer:
This appears to be a bug in Agentforce. You should raise a support ticket to report the issue. However, a workaround can be implemented using prompt engineering within the action instructions. By adding detailed descriptions to your @InvocableVariable
annotations, Agentforce can better interpret the output and render it as a table.
Join our Salesforce training in Chennai to master Admin, Developer, and AI modules with expert guidance, hands-on learning, and free demo classes!!!
Below is a revised version of your class with more explicit annotations:
global inherited sharing class DocumentService {
global class Response {
@InvocableVariable(label='Documents' description='List of Document objects containing details about each document')
public Document[] documents = new Document[] {};
}
global class Document {
@InvocableVariable(label='Content Version Id' description='The unique identifier for the content version of the document')
public Id contentVersionId;
@InvocableVariable(label='Title' description='The title or name of the document')
public String title;
@InvocableVariable(label='Size' description='The size of the document in bytes')
public Integer size;
@InvocableVariable(label='Created Date' description='The date and time when the document was created')
public Datetime createdDate;
}
@InvocableMethod(label='Get Document Responses' description='Returns document details for the given document names')
public static List<Response> getDocumentsResponse(List<String> documentNames) {
List<Response> responses = new List<Response>();
for (String docName : documentNames) {
Response response = new Response();
List<Document> documents = new List<Document>();
for (Integer i = 1; i <= 2; i++) { // Example data
Document doc = new Document();
doc.contentVersionId = Id.valueOf('068Oy000009lyraIAA'.substring(0, 15 - i.toString().length()) + i.toString());
doc.title = docName + ' - Version ' + i;
doc.size = Math.mod(docName.length() * 100 * i, 500000);
doc.createdDate = Datetime.now().addDays(-Math.mod(docName.length() * i, 30));
documents.add(doc);
}
response.documents = documents;
responses.add(response);
}
return responses;
}
}
Code explanation:
The DocumentService
class is a global Apex class designed to retrieve and return details of documents in a structured format. It contains two nested classes: Response
, which holds a list of Document
objects, and Document
, which encapsulates details like ContentVersionId
, Title
, Size
, and CreatedDate
. The getDocumentsResponse
method generates mock document data for each name in the input list, creating two versions of each document with dynamically generated properties, and returns the results in a list of Response
objects.
This approach ensures that Agentforce interprets and renders the data as intended. If the issue persists, Salesforce support can provide further assistance.
Summing Up:
The DocumentService
class is a versatile and extensible framework for retrieving and organizing document metadata in Salesforce. It provides a global structure through its nested Response
and Document
classes, allowing seamless encapsulation of document attributes such as ID, title, size, and creation date. The getDocumentsResponse
method simulates the generation of document data by creating two mock versions for each provided document name, dynamically calculating attributes based on input parameters. This class demonstrates how to use Apex’s object-oriented principles to design reusable and well-organized solutions for managing and sharing document-related data.
Salesforce Training in Chennai: Unlock Your Potential
Elevate your career with our comprehensive Salesforce training in Chennai, designed to equip you with expertise in Admin, Developer, and AI modules. Our program offers unparalleled certification guidance, rigorous interview preparation, and industry-aligned training to ensure you gain a competitive edge. With in-depth modules and expert-led sessions, you’ll build a solid foundation in Salesforce while mastering advanced techniques to meet real-world demands.
Our unique learning approach combines practical hands-on sessions with detailed class notes, enabling you to gain job-ready skills and confidence. Whether you’re starting fresh or upskilling, our training ensures you stand out in the fast-paced Salesforce ecosystem.
Take the first step toward your success by joining our free demo class today and experience the best in Salesforce education!!!