5

Is it possible to create a custom segment condition that you pass in two values into and then perform a conditional expression between two values.

e.g. Create a segmented list if the IndexedContact last visit date was between 5 and 10 days ago?

All the code I've found to help so far uses TypedQueryableOperatorCondition which only checks a single value against a single parameter e.g.:

return this.GetCompareExpression<DateTime>((Expression<Func<IndexedContact, DateTime>>) (c => c.LatestVisitDate), DateUtil.ParseDateTime(this.Value, DateTime.MinValue));

I'm using Sitecore version 8.2 and Email Experience Manager version 3.5

1 Answer 1

4

TypedQueryableOperatorCondition and TypedQueryableStringOperatorCondition are helpers, and are both using Sitecore.Rules.Conditions.WhenCondition<T> behind the scene, so you could implement your own condition based on this.

As an (untested) example:

public class CustomLatestVisitDateCondition<T> : WhenCondition<T>, IQueryableRule<IndexedContact>
    where T: VisitorRuleContext<IndexedContact>
{

    public Expression<Func<IndexedContact, bool>> InitPredicate { protected get; set; }
    public Expression<Func<IndexedContact, bool>> ResultPredicate { get; protected set; }

    public string FromValue { get; set; }
    public string ToValue { get; set; }

    protected override bool Execute(T ruleContext)
    {
        var from = DateUtil.ParseDateTime(FromValue, DateTime.MinValue);
        var to = DateUtil.ParseDateTime(ToValue, DateTime.MinValue);

        this.ApplyFilter(ruleContext, c => c.LatestVisitDate >= from && c.LatestVisitDate <= to);
        return true;
    }

    private void ApplyFilter(T ruleContext, Expression<Func<IndexedContact, bool>> expression)
    {
        ResultPredicate = (InitPredicate ?? ruleContext.Where).And(expression);
        if (InitPredicate != null)
        {
            return;
        }

        ruleContext.Where = ResultPredicate;
    }
}
1
  • Jacob, when I try to do this Sitecore complains at run time that I'm using VisitorRuleContext instead of the plain `RuleContext, thus throwing a TypeLoadException. Is there any reason why I'm not being passed a VisitorRuleContext? Commented Jan 28, 2020 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.