Salesforce CRT-450 Question Answer
A Next Best Action strategy uses an Enhance Element that invokes an Apex method to determine a discount level for a Contact, based on a number of factors.
What is the correct definition of the Apex method?
apex
Copy
@InvocableMethod
global static List> getLevel(List
{ /*implementation*/ }
apex
Copy
@InvocableMethod
global Recommendation getLevel(ContactWrapper input)
{ /*implementation*/ }
apex
Copy
@InvocableMethod
global List> getLevel(List
{ /*implementation*/ }
apex
Copy
@InvocableMethod
global static List
{ /*implementation*/ }
The Answer Is:
DExplanation:
Comprehensive and Detailed Explanation From Exact Extract:
To determine the correct definition of the Apex method for a Next Best Action strategy’s Enhance Element, we need to evaluate the syntax and requirements for an @InvocableMethod used in this context, focusing on its accessibility, input/output types, and compatibility with Next Best Action. Let’s analyze the problem and each option systematically, referencing Salesforce’s official documentation.
Understanding Next Best Action and Enhance Elements:
Next Best Action (NBA): NBA is a Salesforce feature that provides personalized recommendations to users, often used in process automation (e.g., via Flow or Einstein Next Best Action). It evaluates strategies to suggest actions, such as offering a discount to a Contact.
Enhance Element: In NBA, an Enhance Element allows developers to invoke custom logic (e.g., an Apex method) to modify or generate recommendations. The Salesforce Next Best Action documentation states: “An Enhance Element in a Next Best Action strategy can invoke an Apex method to dynamically generate or modify recommendations” (Salesforce Help, Next Best Action).
Apex Method Requirements:
The method must be annotated with @InvocableMethod to be callable from NBA or Flow.
It must be global and static to be accessible outside the class.
For NBA, the method typically returns recommendations as a List> (for bulk processing).
The input can be a List of a custom wrapper class (e.g., ContactWrapper) to pass data like Contact details.
The Apex Developer Guide specifies: “An @InvocableMethod must be global, static, and can take a List of inputs and return a List of outputs” (Salesforce Apex Developer Guide, InvocableMethod Annotation).
Requirement Analysis:
Purpose: The method determines a discount level for a Contact, which suggests it generates or modifies a recommendation (e.g., a discount offer).
Input: The method likely takes a List
Output: Since this is for NBA, the method should return a List
Enhance Element Specifics: An Enhance Element typically expects a List>, unless bulkified in a specific way. However, the method must support bulkification (taking a List as input and returning a List).
Evaluating the Options:
A.
apex
Copy
@InvocableMethod
global static List> getLevel(List
{ /*implementation*/ }
Accessibility: global static and @InvocableMethod are correct for an invocable method.
Input: List
Output: Returns List>, which is a nested list. The Apex Developer Guide notes: “An @InvocableMethod can return a nested List, but for Flow or NBA, the expected output is typically List
List<Recommendation>
to map directly to recommendations, not a nested list. A nested list (List<List<Recommendation>>
) is more common in Flows when processing multiple outputs for multiple inputs, but for NBA, this is not the standard pattern for an Enhance Element.
Conclusion: Incorrect, as the nested List> return type does not align with the typical expectation of an Enhance Element in NBA, which expects List
B.
apex
Copy
@InvocableMethod
global Recommendation getLevel(ContactWrapper input)
{ /*implementation*/ }
Accessibility: Uses @InvocableMethod and global, but the method is not static. The Apex Developer Guide states: “An @InvocableMethod must be static to be callable from Flow or Next Best Action” (Salesforce Apex Developer Guide, InvocableMethod Annotation). This results in a compilation error.
Input: Takes a single ContactWrapper, not a List
Output: Returns a single Recommendation, not a List
Conclusion: Incorrect due to missing static keyword, and both input and output violate the bulkification requirement (must use List).
C.
apex
Copy
@InvocableMethod
global List> getLevel(List
{ /*implementation*/ }
Accessibility: Uses @InvocableMethod and global, but the method is not static, resulting in a compilation error (same issue as option B).
Input: List
Output: Returns List>, which, as noted in option A, is not the typical return type for an NBA Enhance Element. It expects List
Conclusion: Incorrect due to missing static keyword and the nested List> return type, which is not ideal for NBA Enhance Elements.
D.
apex
Copy
@InvocableMethod
global static List
{ /*implementation*/ }
Accessibility: global static and @InvocableMethod are correct, making the method accessible to NBA.
Input: List
Output: Returns List
Conclusion: Correct, as it meets all requirements: proper accessibility, bulkified input/output, and the expected return type for NBA.
Why Option D is Correct:
Option D is correct because:
It uses @InvocableMethod, global, and static, making the method callable from a Next Best Action Enhance Element.
The input List
The output List
This aligns with Salesforce best practices for invocable methods in process automation, as outlined in the Apex Developer Guide and Next Best Action documentation.
Example for Clarity:
Here’s how option D might be implemented:
apex
Copy
global class DiscountCalculator {
@InvocableMethod(label='Get Discount Level' description='Determines discount level for a Contact')
global static List
List
for (ContactWrapper wrapper : input) {
// Example logic to determine discount level
Decimal discountLevel = 0.1; // 10% discount based on factors
Recommendation rec = new Recommendation();
rec.Name = 'Discount Offer';
rec.Description = 'Offer a ' + (discountLevel * 100) + '% discount to ' + wrapper.contactName;
rec.ActionReference = 'ApplyDiscount'; // Reference to a Flow or action
recommendations.add(rec);
}
return recommendations;
}
}
global class ContactWrapper {
@InvocableVariable
global String contactName;
@InvocableVariable
global Id contactId;
// Add other factors as InvocableVariables
}
Usage in NBA: The Enhance Element in the Next Best Action strategy calls getLevel, passing a List
Handling Typos:
The options are syntactically correct in the provided image, with no typos to address.
The question assumes ContactWrapper is a properly defined class with @InvocableVariable properties, which is a reasonable assumption for the context.
Option D’s ListRecommendation in the original question appears to be a typo (missing < and >). The correct syntax is List