Now that we’ve gone over some basic functions, we’ll go over how you can use more than one function in a given rule. We touched on this a bit in our last article, but let’s take a deeper dive into combining — or “nesting” — functions.

Basics of Combining or Nesting Functions

When you create a business rule, you may nest functions inside of other functions. When you do this, you’ll be using the results of the inner nested functions as arguments, or bits of data, that the outer function needs to run properly.

Let’s take a very simple example:

  • Rule 1:
    IFBLANK($itembrand,$itemmanufacturer)
  • Rule 2:
    IF($itembrand=”Acme Co.”, “Acme”, $itembrand)

Let’s combine these by inserting Rule 2 into part of Rule 1 in place of $itembrand:

  • IFBLANK( Insert Rule 1 here ,$itemmanufacturer)

The resulting rule looks like this:

  • IFBLANK( IF($itembrand=”Acme Co.”, “Acme”, $itembrand) ,$itemmanufacturer)

When nesting rules in this way, it’s often easier to write and test them separately, and then combine them. Let’s take a deeper dive.

Work from the Inside Out

In addition to writing and testing parts separately, it’s also best to work from the inside out when forming your nested rules.

For example, let’s say you want to add brand name to the beginning of your item titles. And, perhaps you also occasionally need to correct the brand name. In an earlier blog post, we already wrote a rule to correct the brand name using a SELECTCASE expression.

Here’s that rule:

  • SELECTCASE(
    CONTAINS($itembrand,”Acme”),”Acme Tools”,
    CONTAINS($itembrand, “Nike”), “Nike Footwear”,
    $itembrand
    )

The SELECTCASE rule above does the following:

  • If the field named ItemBrand contains “Acme” use “Acme Tools”
  • Otherwise if ItemBrand contains Nike, use “Nike Footwear”.
  • If all else fails, just use ItemBrand.

After testing this rule in the preview to ensure it behaves as desired, let’s now combine it with a JOIN function to get the desired output of title with brand added to the beginning of the string.

Here’s the new combined rule:

  • JOIN(“ “,
    SELECTCASE(
    CONTAINS($itembrand,”Acme”),”Acme Tools”,
    CONTAINS($itembrand, “Nike”), “Nike Footwear”,
    $itembrand
    ),
    $itemtitle)

Now the rule says this:

  • Take the output of the SELECTCASE function
  • And join the above output to it, using a space character, with the field named ItemTitle.

Here’s what it looks like in the Business Rule Preview Pane:

But wait, there’s more!

For the example SKU, the title already contains the brand name. So, we’ve duplicated the brand name and ended up with an undesirable output:

“Nike Footwear Nike Footwear Flex Run men’s running shoe”

In order to correct it, we need to add another layer of logic to our rule to check if the title already contains the brand name. If it already contains the brand name, don’t add it again. It can be tricky to figure out where to add this new logic to an existing rule.

Here’s what we do know:

  • SELECTCASE is the part of the rule that is deciding what to use for brand name in the JOIN.
  • SELECTCASE evaluates each comparison in order that it appears in the statement. Once it finds a match, it stops looking for any more.
  • So, it makes sense to add that extra “is brand in title already” check as the first expression within the SELECTCASE. That way, if it finds brand in title already, stop and don’t do anything here.

The new rule looks like this:

  • JOIN(” “,
    SELECTCASE(
    CONTAINS($itemtitle,$itembrand),””,
    CONTAINS($itembrand,”Acme”),”Acme Tools”,
    CONTAINS($itembrand, “Nike”), “Nike Footwear”,
    $itembrand
    ),
    $itemtitle)

By adding the line in highlighted text above, we’ve changed the SELECTCASE part of the rule to do this:

  • If the title already contains the brand, just use blank (in other words, do nothing).
  • If the field named itembrand contains “Acme” use Acme Tools
  • Otherwise if itembrand contains Nike, use Nike Footwear.
  • If all else fails, just use itembrand.

Now the entire rule says this:

  • Take the output of the SELECTCASE function
  • Take the output of the SELECTCASE function
  • And join the above, using a space character, with the field named itemtitle.

Here’s the Preview pane now:

That looks much better.

Let’s Go for a Bonus Round

What if you want to have the entire string use proper casing? Just add one more layer to our nested function.

And what if you’re not sure what function to use for proper case?

Let’s search the business rule functions list for available functions. This can be done right inside the Rule Editor! Just click the “Functions” tab, then type “proper” into the search box. Click the [+] symbol to see example syntax.

So, now, we want to take the output of our prior work and plug it into this “proper function”:

  • PROPER ( my other function goes here )

The new rule looks like this:

  • PROPER(
    JOIN(” “,
    SELECTCASE(
    CONTAINS($itemtitle,$itembrand),””,
    CONTAINS($itembrand,”Acme”),”Acme Tools”,
    CONTAINS($itembrand, “Nike”), “Nike Footwear”,
    $itembrand
    ),
    $itemtitle)
    )

The final preview looks like this:

 

Looks great, don’t you agree? This trial and error, layer upon layer technique is often how business rules are written.

Pro Tip: The Importance of Order

By using “Proper” on the outside of everything like this, it ensures all strings from al lnested expression will receive the desired casing. However, what if some of your items have a brand name (such as adidas) that you don’t want capitalized? In that case, you could wrap just the title part of the rule in a “Proper” expression to leave the brand as-is, like so:

  • JOIN(” “,
    SELECTCASE(
    CONTAINS($itemtitle,$itembrand),””,
    CONTAINS($itembrand,”Acme”),”Acme Tools”,
    CONTAINS($itembrand, “Nike”), “Nike Footwear”,
    $itembrand
    ),
    PROPER($itemtitle)
    )

As you think of more ways to transform your data for optimization, combining business rule functions is a powerful skill to add to your toolbox.