Salesforce Flow is a great tool to automate business processes, and the best part is it can combine the MVC all together in one go. So we can build a flow with screens to interact with users and add the logic to handle the data. However, changes are inevitable for all kinds of processes, which leads to the question of how to make flows easy to maintain and adapt to changes.
For example, a client wants to collect some diet preferences when users launch the flow. It will be a list of options to pick from. However, the business changes the options frequently, and based on the chosen option, users need to see different following screens like different dishes. The most straightforward way is to build everything into the flow itself, or maybe pull the options from a picklist field or custom metadata types (CMDT). But with more questions involved, this pattern may get difficult to handle.
Let me explain with some pictures.
For example, I have a form with 3 questions. Question No.1 has 2 options A and B. If the user picks A, then the next one will be Question No.2. Option B leads to Question No.3.
You can easily put the logic into flow at this scale, and there is not much difference with the new pattern. However, imagine you have more questions like this:
You can still do it in the flow directly, but it is getting messy, and harder, especially for maintenance. And what about you have 15 questions, or more? You may end up with something like this:
It might look something cool to show off, but definitely not something nice to maintain, at least not for me. So what alternative can I have?
Solution
http://unofficialsf.com/scaling-to-thousands-of-screens-with-lightning-flow/
Inspired by the great article above, I recently figured out a new way to design flows. All it requires is to save the logic into 2 custom objects or CMDT (it usually requires admin to modify and does not support Rich Text) as records.
I named them Question Node (questions) and Question Node Outcome (answers).
Both of them need a Label field to hold the text of the question/answer. I used rich text type, so I can have more flexibility to control the format.
The Question Node needs to have a type picklist field, to distinguish if it's a choice question or some other types like number or date.
The Queston Node Outcome has two lookup fields to Queston Node, and they are the key for this pattern. I named them Parent Node and Target Node.
In this pattern, Each question is a Queston Node record, and each answer is a Question Node Outcome record. For the 3 question example above, I need 3 records of Question Node for each question. For Question No.1, there are 2 Queston Node Outcome records representing A and B. Both have Question No.1 as Parent Node, but Question No.2 and No.3 as Target Node respectively.
With the new pattern, no matter you have 3 questions or 300, the flow remains the same like this:
All the complexity is handled in those records because each Node Question Outcome has a target Question Node. When user runs the above flow, an initial Question Node record is passed as an input, and then the flow dynamically "Get" the next question based on the user's interaction. And the magic comes from the Record Choice Set resource in the flow.
In the above example, the Quesiion No.1 is the curNode, and this element will get Option A and B as choices. Depending on the user's choice, the next Node will be set as Question No.2 or No.3.Remember I mentioned the type field on the question? It provides the criteria for the flow to identify the corresponding data to proceed. So other than choices, we can potentially collect number, date, and text answers as well. And that's why I have some decision nodes to check types in the above flow.
But how to capture the data collected in the flow? That's those assignment elements for. I have a text variable to be edited in the assignment, so each question-answer pair will be added to it as a new line. And it will generate a large string like this at the end:
Question1 Answer1
Queston2 Answer2
QuestionN AnswerN
Based on the use case, you may need to create or update records using the above information, but that needs some Apex involved to parse the data.
Limitations
The biggest limitation is that you can only place one question using Record Choice Set on each screen. Otherwise, the variable related to the record will only hold the last choice you picked.
It is hard to allow multi-select, but it is possible with Apex because you need to parse the selected answer and reverse it back to a collection variable.
Variety
This pattern can be used by combining the normal way to build flow. You can use just one object to hold those parameters that change frequently, and still build the logic into flow. So you can easily have the DML features or bypass the limitation of one question on each screen.
This article only shows the core implementation of this pattern. I have some ideas to enhance it, like adding field names into those choices, so potentially I build a JSON string at the end, which is easier to create and update records of any object. Anyway, the sky is the limit.