Option filters are a simple yet powerful way to express complex business logic. While referring to a matrix of options solves many problems, you may want to filter the data in that matrix before it is offered to your user. Apply business rules to your matrix via a query rule, very similar to the way SQL works.
This is part of a series of help documents on how to use option filters. For the full list, see the "Related Articles" section below.
Before you begin...
- Be familiar with the basic concept of option filters, as described on the Option Filter page.
- Know the basics of Snap, our programming language, and have already built some simple value rules or validation rules for experience in using Snap.
- Complete the "Option Filters 1: Basic Setup" walkthrough, so you have a working example of an option filter driven by a local table of data.
Overview
In this walkthrough, we will:
Review the basic matrix of choices from the previous walkthrough.
In the previous walkthrough, we learned how to represent a table of data as a series of fields, one for each column. Since we represented the entire table, there was no filter in place, and no Snap code was required.
Data | User Interface |
---|---|
Three columns of data... |
...appears as three fields in the UI. |
Apply a business rule to filter the choices presented to your user.
Using the entire matrix of data is nice, but you may want to filter out some of the data in your table. Consider the following scenario. Our supply chain department contacts us, saying "All synthetic materials are on back-order. Do not show these materials until further notice." We have two solutions:
- We could add a validation rule, warning anyone who sets the Family field to synthetic that these products aren't available. The benefit: our users are aware we usually sell these products, but not today. The drawback: we are offering something which we cannot fulfill.
- We can add a query to our option filter, so synthetic choices don't even appear. The benefit, we don't give our users errors for simply selecting a product that's wrong... at least for today.
For the purposes of this exercise, let's do the second option. Let's add a query.
Task | Walkthrough Example | |
---|---|---|
1 | Add a query to your option filter. |
|
2 | Define the source of your query. |
|
3 | Test your query. |
This is a very simple query. It's getting all the data from the same table, with no filters or logic in place yet. Therefore, it should behave just like it did before you started step 1. Run your configurator to be sure. |
4 | Add logic to the new query. | Again, click on the "filter rule" listed below your option filter to edit the filter. To meet the business rules of our scenario, we'll edit the query block:
|
5 | Test the new logic in your query. |
Run your configurator. You should notice that no synthetic items appear. Remember, comparisons are exact, so it's easy to make these mistakes:
|
Use boolean logic to incude a second business rule.
Let's continue our scenario. What if supply chain contacts us again, saying "All metallic materials are also on back-order. Do not show synthetic or metallic materials until further notice." We can do that!
Task | Walkthrough Example | |
---|---|---|
1 | Add boolean logic to the query. | Again, click on the "filter rule" listed below your option filter to edit the filter. To meet the business rules of our scenario, we'll edit the query block:
|
2 | Test your new query. | Run your configurator. You should notice that no synthetic or metallic items appear. |
Add conditional logic based on information from the configurator itself.
The fast changes you made are causing quite a stir in the Sales Department. After some discussions with Supply Chain, they all have agreed to the following: if a new "Priority Request" field is set to true, then allow the user to select metallic materials. But metallic should be hidden otherwise.
Task | Walkthrough Example | |
---|---|---|
1 | Create the new field. | In your UI, create a new boolean field to track if the user is a priority user. Give it the name "fIsPriorityRequest", and either a toggle or checkbox control. (In a real application, we would not leave this visible and easy for everyone to edit.) You can give it a shorter label, such as "Priority". |
2 | Add this new field to the query. | You can refer to any field in the configurator using the GetField block. And you can use conditional logic within your query, to make certain parts of it be applied or be ignored.
|
3 | Test your work. | Now, depending on the status of this new true/false field, choices will appear or disappear. |
Queries can become complex
By combining these three blocks in various combinations, you can create queries to satisfy the most complex business needs. If you find your queries are getting too difficult to manage, consider these:
- Consider "chaining" your option filters
Instead of having all your logic in once place, you can factor your logic into more manageable sections.
- Consider other option filter techniques, like the "truth table"
Other option filter techniques let you store business logic in tables, rather than in Snap code. These logic tables are often easier to understand and manage.
See the related articles below for more information.