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

3

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;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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