What are three considerations when using the @InvocableMethod annotation in Apex?
Choose 3 answers
Correct Answer: A,C,E
The @InvocableMethod annotation is used to identify methods that can be run as invocable actions from a flow, a bot, or an NBA. There are some considerations when using this annotation in Apex, such as:
* A method using the @InvocableMethod annotation must define a return value, even if it is null. This is because the invocable action must return a value to the flow, bot, or NBA that invokes it.
* A method using the @InvocableMethod annotation can have only one input parameter, which can be a list of a primitive data type, a list of a specific sObject type, or a list of a generic sObject type. This is because the invocable action can accept only one input value from the flow, bot, or NBA that invokes it.
* A method using the @InvocableMethod annotation must be declared as static, because it is not associated with a specific instance of the class that defines it. Static methods can be invoked without
* creating an object of the class.
* A method using the @InvocableMethod annotation can be declared as either public or global, depending on the visibility of the invocable action. Public methods are visible within the same namespace, while global methods are visible across namespaces.
* Only one method using the @InvocableMethod annotation can be defined per Apex class, because each invocable action is associated with a single Apex class. Multiple invocable methods in the same class would cause ambiguity and confusion.
References: InvocableMethod Annotation | Apex Developer Guide, How to Invoke Apex from Flows Using InvocableMethod Annotation
Recent Comments (The most recent comments are at the top.)
C D E are the correct answers.
C. A method using the @InvocableMethod annotation must be declared as static: This is a requirement for methods annotated with @InvocableMethod, as they are intended to be called from outside of the Apex class, often through declarative tools like Process Builder or Flow.
• D. A method using the @InvocableMethod annotation can be declared as Public or Global: This is correct because @InvocableMethod methods must be accessible to the framework that invokes them, and thus can be declared as public or global.
• E. Only one method using the @InvocableMethod annotation can be defined per Apex class: This limitation is imposed to ensure simplicity and clarity when invoking methods from declarative tools.
Incorrect Considerations:
• A. A method using the @InvocableMethod annotation must define a return value: This is not true. Methods annotated with @InvocableMethod do not need to define a return value; they can have a void return type.
https://developer.salesforce.com/docs/atlas.en-us.234.0.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htm
c : "The invocable method MUST be static and public or global, and its class must be an outer class."
d : "The invocable method must be static and public OR global, and its class must be an outer class."
e : "Only one method in a class can have the InvocableMethod annotation."