In my previous post I’ve described what Custom Values are in Marketing Automation, and how you can use them. In this post I’m showing you an example how to create a generic condition so you can use the custom values easily in every Automation Plan.
To give the content-editor the maximum amount of flexibility, you’d have to create at least the conditions as specified below:
- Compare a custom value to a given number
- Compare a custom value to a given date
- Compare a custom value to a given string
- Compare a custom value to a given item
The Sitecore Documentation properly explains how to create a new segmentation rule, so I’m keeping it simple this time by just showing how to access the custom values for one of the conditions. If you need any help with another condition, just send me a message.
Comparing the Custom Value to a given item
To compare the custom value (the backed value is a string representation of an ID), you can use the following text when you create the condition within Sitecore:
where the [ValueName,,,custom value] is [ExpectedValue,Tree,root=/sitecore,item]
This gives the content editor to type the name of the Custom Value, and to select an item from the content tree. As code, you can use the snippet below:
using System; using Sitecore.Framework.Rules; using Sitecore.Xdb.MarketingAutomation.Core.Processing.Plan; namespace Example { public class CustomValueIdMatches : ICondition { public string ValueName { get; set; } public string ExpectedValue { get; set; } public bool Evaluate(IRuleExecutionContext context) { var contactProcessingContext = context.Fact<IContactProcessingContext>(); if (!Guid.TryParse(contactProcessingContext.GetCustomValue(ValueName.ToLowerInvariant()), out var givenValue)) { return false; } if (!Guid.TryParse(ExpectedValue, out var expectedValue)) { return false; } return expectedValue == givenValue; } } }
Summary
As you can see it’s really easy to use the Custom Values in your own conditions, and it gives content-editors all the tools they need to create amazing Automation Plans without the further need for custom development.