If you're an experienced Apex developer and have wondered if there?s a better way to manage multi-step logic that involves both DML and callouts to external systems, this session is for you. The Flow Factory is a stateful design pattern that provides structure for processing flowchart style business logic in Apex. This begins with flow-specific inputs and results in a chronological record of executed steps to iterate for final processing. It has roots in both the State pattern and the Factory Method and is particularly useful for managing more than one callout in a single execution context.
4. Object-oriented design patterns
▪ Program to an interface, not an implementation
▪
Strive for loosely-coupled designs
▪
Encapsulate what varies
▪
Remember the open-closed principle
8. The problem grew
Course
Complete
In rewards
program?
In US registry?
Update
Salesforce
N
Create
profile
Y
Update
profile
Y
Add rewards
points
N
In international
registry?
N
Y
Update profile
Done
Welcome
email
Rewards
email
10. Deferring DML and email
Course
Complete
In rewards
program?
In US registry?
Update
Salesforce
N
Create
profile
Y
Update
profile
Y
Add rewards
points
N
In international
registry?
N
Y
Update profile
Done
Welcome
email
Rewards
email
11. The Flow Factory
▪ Flow-specific inputs
▪ Executed steps collection
▪ Steps create & return next steps
▪
Inversion of Control
▪ Two pass execution
▪
Pass 1: Step.processStep()
▪
Pass 2: Step.finish()
12. Flow of the flow
Pass 1
Step 1
Step 2
Step 3
Step 4
▪Add self to list
▪Process work
▪Create next
▪…
▪…
▪…
▪…
▪...
▪…
▪...
▪…
▪Return null
Step 1
Step 2
Step 3
Step 4
▪Final processing
▪…
▪…
▪…
Pass 2 (DML)
21. Pass 2
finish()
Course
Update Salesforce
(DML)Complete
In US registry?
Update
Salesforce
Save callout response
Send email
N
In international
registry?
finish()
N
Save callout response
N
finish()
In rewards
program?
Create
profile
Y
Update
profile
Y
Add rewards
finish() points
Save callout response
Send email
Y
Update profile
Done
Welcome
email
Rewards
email