Sub topic 5
Designing request and response classes
This chapter assumes you can write basic Apex classes, know what a list and a SOQL query are, and have seen the @InvocableMethod and @InvocableVariable annotations. It covers how to shape the data that goes into and out of your method.
Why wrapper classes
An invocable method can take only one parameter, and that parameter must be a list. A list of primitives works for simple cases. Once you need several inputs, such as an account ID and a subject, you need a wrapper class. A wrapper class is a small Apex class whose fields carry the values. Each field you want Flow to see gets @InvocableVariable.
The same idea applies to output. A response class lets you return a record ID, a success flag, and an error message for each input.
A request class
public class Request {
@InvocableVariable(label='Account Id' description='Account that the task relates to' required=true)
public Id accountId;
@InvocableVariable(label='Task Subject' description='Defaults to Follow up if blank')
public String subject;
}Points to follow:
- Fields must be
public(orglobalin a managed package) and carry@InvocableVariable. Fields without the annotation are invisible to Flow. required=truemakes Flow Builder insist on a value for that input. Optional fields arrive asnullif the flow does not set them, so check them in your code.labelanddescriptionappear in Flow Builder. Write them for the admin who will configure the action.- Use supported types: primitives,
Id,sObjecttypes, collections of these, and other Apex-defined types in recent API versions.
A response class
public class Result {
@InvocableVariable(label='Task Id')
public Id taskId;
@InvocableVariable(label='Is Success')
public Boolean isSuccess;
@InvocableVariable(label='Error Message')
public String errorMessage;
}Output fields appear as values the flow can store in variables or use in later elements. Including isSuccess and errorMessage lets a flow branch on a per-record outcome with a Decision element, instead of failing everything.
How the pieces fit
Putting it together
The method signature uses both classes. Because the output list must match the input list in size and order, the method builds the results by walking the save results by index.
public with sharing class AccountTaskCreator {
public class Request {
@InvocableVariable(label='Account Id' description='Account that the task relates to' required=true)
public Id accountId;
@InvocableVariable(label='Task Subject' description='Defaults to Follow up if blank')
public String subject;
}
public class Result {
@InvocableVariable(label='Task Id')
public Id taskId;
@InvocableVariable(label='Is Success')
public Boolean isSuccess;
@InvocableVariable(label='Error Message')
public String errorMessage;
}
@InvocableMethod(
label='Create Follow-up Tasks'
description='Creates one follow-up task per request and returns one result per request.'
category='Account'
)
public static List<Result> createTasks(List<Request> requests) {
List<Task> tasks = new List<Task>();
for (Request req : requests) {
tasks.add(new Task(
WhatId = req.accountId,
Subject = String.isBlank(req.subject) ? 'Follow up' : req.subject,
ActivityDate = Date.today().addDays(7)
));
}
List<Database.SaveResult> saveResults = Database.insert(tasks, false);
List<Result> results = new List<Result>();
for (Integer i = 0; i < saveResults.size(); i++) {
Result res = new Result();
res.isSuccess = saveResults[i].isSuccess();
if (res.isSuccess) {
res.taskId = saveResults[i].getId();
} else {
res.errorMessage = saveResults[i].getErrors()[0].getMessage();
}
results.add(res);
}
return results;
}
}Both classes are nested inside the outer class. Flow Builder shows them as the action's inputs and outputs. Database.insert(tasks, false) allows partial success, so one bad record does not block the others, and each failure is reported in its own Result.
Common design mistakes
- Forgetting the annotation on a field. The field compiles but does not appear in Flow Builder.
- Returning a different number of results than requests. Flow matches outputs to interviews by position, so a shorter or reordered list gives wrong values to the wrong interviews.
- Making everything required. Required inputs are strict. Mark a field required only when the method cannot work without it.
- Skipping
nullchecks on optional inputs. An unset field isnull, and calling a method on it throws an exception. - Putting logic in the wrapper classes. Keep them as plain data holders. Put behavior in the method or a helper class.
Exercise
Your method receives a list of 50 requests, but your code skips two invalid ones and returns a list of 48 results. What goes wrong, and how would you fix it?
Show answer
Flow matches results to interviews by position, so the results shift and some interviews get another interview's output. Fix it by adding one Result for every Request, in the same order. For an invalid request, add a Result with isSuccess set to false and an errorMessage that explains why.