contact@skillcore.net

Sitecore Rules Engine - decimal macro

Posted 16 October 2016 by Marek Musielak

sitecore decimal macro double macro floating point number macro

Some time ago I worked on a project where we had access to plenty of numeric data stored in the fields of our content items. The plan was to use this data to personalize the content on the site. Some of the data was decimal numbers. I hoped to use a built-in Sitecore macro which would allow me to create a condition like where the decimal value of field compares to number but to my surprise, there was no macro for decimal or any other floating point number.

I checked what macros are available in Sitecore out of the box. There were macros for Tree and Treelist which both allow item selection. There was IntegerMacro and DateTimeMacro, there was even PositiveIntegerMacro and GenderMacro. But there was no DoubleMacro, DecimalMacro or whatever we could call a macro which uses floating point numbers.

Ok, not a big deal. Lets check how IntegerMacro works and write my custom macro for decimal numbers. When I decompiled IntegerMacro and checked its code, I realized that I can create my custom condition without any coding. IntegerMacro accepts additional parameters for validation regex and validation text which allows to create a condition which accepts floating point numbers.

Syntax of the parametrized list in the condition is:

[NameOfTheProperty,MacroName,AdditionalParameters,DefaultText]

so e.g. for a condition which uses Tree macro, parametrized list in the condition can look like this:

[value,Tree,root=/sitecore/system/Settings/Analytics/Lookups/Countries,specific country]

And that's the 3rd parameters which allows custom logic for IntegerMacro. You pass there 2 values:

  1. validation - url encoded regex for the input
  2. validationText - text used when the value of the input is incorrect

If they are not specified, IntegerMacro uses ^0$|^\\-?[1-9]{1}[0-9]{0,8}$ and Please enter a valid integer value. parameters. So what I've done? I created new condition and passed encoded ^\d+(\.\d+)?$ and Plase enter a valid decimal value. to the macro using:

where the decimal value of 
 [FieldId,Tree,root=/sitecore/system/Settings/Rules/Definitions/My Custom Fields,field]
 [operatorid,Operator,,compares to] 
 [value,Integer,validation=%5E%5Cd%2B(%5C.%5Cd%2B)%3F%24&validationText=Please enter a valid decimal value,number]

And that's it. Now when I used the condition, input field which allowed only integers before, accepts decimal numbers now.

sitecore decimal macro double macro floating point number macro

And if you're interested in Sitecore Rules Engine, check out my previous blog post about Sitecore Insert Options Rules.