Sub topic 14
Combining Flows with Apex, Subflows, and Prompt Templates
This chapter assumes you can build an autolaunched flow, define input and output variables, and register a flow as an action in Agent Builder. Those steps are covered in earlier chapters. Here you will see how to extend one flow-based action with other building blocks, and when each is worth using.
Why combine building blocks
A single flow can read records, decide, and update data. Some jobs need more. You may need logic that Flow cannot express cleanly, a piece of logic that several actions share, or text that a language model should write. Flow can call Apex, other flows, and prompt templates, so the agent still sees one action while the work is split behind it.
| Building block | Use it when | Called from the flow with |
|---|---|---|
| Apex invocable method | Logic is too complex or too slow for Flow elements, or you need a callout or bulk processing | Action element |
| Subflow | Several flows repeat the same steps | Subflow element |
| Prompt template | The result must be generated text, such as a summary or a drafted reply | Action element |
Calling Apex from a flow
An invocable method is an Apex method marked so that Flow can call it. It takes a list of request objects and returns a list of results, because Flow may call it for several records at once.
public with sharing class CaseAgeCalculator {
public class Request {
@InvocableVariable(label='Case Id' description='The Id of the case.' required=true)
public Id caseId;
}
public class Result {
@InvocableVariable(label='Age In Days' description='Days since the case was opened.')
public Integer ageInDays;
}
@InvocableMethod(label='Get Case Age' description='Returns how many days a case has been open.')
public static List<Result> getAge(List<Request> requests) {
Set<Id> ids = new Set<Id>();
for (Request r : requests) {
ids.add(r.caseId);
}
Map<Id, Case> cases = new Map<Id, Case>(
[SELECT Id, CreatedDate FROM Case WHERE Id IN :ids]
);
List<Result> results = new List<Result>();
for (Request r : requests) {
Result res = new Result();
Case c = cases.get(r.caseId);
if (c != null) {
res.ageInDays = Date.valueOf(c.CreatedDate).daysBetween(Date.today());
}
results.add(res);
}
return results;
}
}The results list must be in the same order as the requests. The with sharing keyword makes the class respect the running user's record access, which matters for the run context you chose earlier. In the flow, add an Action element, pick the method, and map its inputs and outputs to flow variables.
Splitting shared logic into subflows
If three actions all look up a case by number, build that lookup once as an autolaunched flow and call it from each action flow with a Subflow element. Give the subflow clear input and output variables, and return a status message on failure so the parent flow can pass it on.
Keep in mind that the agent only registers the parent flow. The subflow does not need to be an action, and its variable descriptions are for builders, not for the agent.
Using a prompt template
A prompt template, built in Prompt Builder, holds instructions and merge fields for a language model. A flow can call a template and receive the generated text in an output variable. For example, a flow could gather a case's details and ask a template to draft a plain-language summary, then return that text as caseSummary.
Use this when the wording matters. Do not use it for lookups or calculations, where a Get Records element or Apex gives a repeatable answer.
Gotchas
- Apex and flow limits still apply. A flow that calls Apex shares the same transaction limits.
- A change to an Apex method's inputs or outputs can break the flows that call it. Deploy them together.
- Each added layer is another thing to test. Test subflows and Apex alone before testing the action in the agent preview.
Exercise
Your team has four action flows that each need to find a case by case number and return an error message if none is found. Which building block do you use for the shared lookup, and which element calls it?
Show answer
Build the lookup once as an autolaunched subflow with a case number input and status and error outputs. Each action flow calls it with a Subflow element and passes the error message through to its own output variable.