Welcome back to Business Rules 101, a new series of blog posts designed to demystify the process of writing and understanding your own business rules. This week, we’re adding flexibility to the IF function presented in the last installment with two additional conditional statements: the CONTAINS and SELECTCASE functions.
The CONTAINS function allows you to look for a string of characters within an attribute. Below is an example:
CONTAINS(“Girl scout cookies”, “scout”)
This business rule looks for the string “scout” inside the phrase “Girl scout cookies”, and returns either a true or false value. In this case, the result would be true.
The comparison is case sensitive, so while the above rule is true, this one is false:
CONTAINS(“Girl scout cookies”, “Scout”).
The CONTAINS function can be used to write a more general form of the example IF statement from the first blog. As a reminder, the rule used was:
IF($itembrand=”Acme Co.”, “Acme”, $itembrand)
You may find that your Acme products have the brand listed in three different ways: ”Acme Co.”, ”Acme Inc”, or ”Acme Toys”. To ensure all versions show as “Acme” on the given marketplace, this rule could be used:
IF(CONTAINS ($itembrand, “Acme”), “Acme”, $itembrand)
As long as “Acme” is found somewhere in the brand attribute, the value sent to the marketplace will be “Acme.”
But what if we have multiple conditions to look for? IF statements can be stacked together to handle this, but it quickly becomes hard to read and harder to support down the road.
This is when the SELECTCASE function can be used. The basic syntax is:
SELECTCASE(Condition1, Value1, Condition2, Value2, Condition3, Value3, Condition4, Value4, Condition5, Value5, Default)
Up to five Condition/Value pairs can be included. Once the first true condition is found, no additional ones will be considered, so SELECTCASE conditions should be entered in order from most to least specific. For better readability, whitespace can be added:
Using the CONTAINS function within a SELECTCASE function might look like this:
CONTAINS($itembrand, “Nike”), “Nike Footwear”,
The above function essentially says:
If “Acme” is found in $itembrand, Use “Acme”.
If not, if “Nike” is found in $itembrand, Use “Nike Footwear”.
If nothing else is True, use the value in $itembrand.
Things to Consider
To close this chapter of the blog, I would like to encourage you to make your rules readable. The example given above will function in either of the formats shown below (click images to enlarge).
As business rules get more complex, they become more difficult to adjust in the future. This is particularly true if the original creator of the rule is not doing the editing. Making a rule that can easily be understood will make the business rule much easier to maintain in the future.
The Business Rule Editor does have a tool to help with this. The pencil icon located in the top right corner of the Editor window will reformat the rule. The results of using the tool on rule “Test2” is shown below (click image to enlarge):
Remember, this is the second of an ongoing series that’ll break down the various ways that you can use business rules to customize and transform your data. If you missed the first lesson and feel a little lost, make sure to read Business Rules 101: Introduction to Business Rules to get caught up. If you want to learn more and don’t want to wait two weeks for the next installment of the series, you can check out our Strategy and Support Center.