After completing this unit, you'll be able to:
Most agent actions run a flow or a standard action. Reach for Apex when the logic is complex, when you need to call an external service with custom handling, or when the result has to be computed exactly the same way every time.
An agent calls Apex through an invocable method: a static method annotated with @InvocableMethod. Inputs and outputs are described with @InvocableVariable, so the agent knows what to send and what it gets back.
public with sharing class LeadScore {
@InvocableMethod(label='Score Lead' description='Returns a fit score for a lead')
public static List<Integer> score(List<Id> leadIds) {
// compute and return one score per lead
}
}1When should an agent action use Apex instead of Flow?
2How does an agent call Apex?