8

Does Sitecore support store booleans in a custom facet?

I have the following interfaces:

using Sitecore.Analytics.Model.Framework;
using System;

    namespace MySite.Models.Facets
    {
        public interface IAdditionalInfoCommunicationElement : IElement
        {
            DateTime Sent { get; set; }
            string ContactType { get; set; }
            string Message { get; set; }
        }
    }

using Sitecore.Analytics.Model.Framework;

namespace MySite.Models.Facets
{

    public interface IAdditionalInfoFacet : IFacet
    {
        string Company { get; set; }
        string RevenueRange { get; set; }
        string Industry { get; set; }
        string SubIndustry { get; set; }
        IElementCollection<IAdditionalInfoCommunicationElement> Communications { get; }
        bool OptIn { get; set; }
    }
}

and the following classes:

using Sitecore.Analytics.Model.Framework;
using System;

namespace MySite.Models.Facets
{
    [Serializable]
    public class AdditionalInfoCommunicationElement: Element, IAdditionalInfoCommunicationElement
    {
        private const string FIELD_SENT = "Sent";
        private const string FIELD_CONTACT_TYPE = "Contact Type";
        private const string FIELD_MESSAGE = "Message";

        public AdditionalInfoCommunicationElement()
        {
            EnsureAttribute<DateTime>(FIELD_SENT);
            EnsureAttribute<string>(FIELD_CONTACT_TYPE);
            EnsureAttribute<string>(FIELD_MESSAGE);
        }

        public DateTime Sent
        {
            get { return GetAttribute<DateTime>(FIELD_SENT); }
            set { SetAttribute(FIELD_SENT, value); }
        }

        public string ContactType
        {
            get { return GetAttribute<string>(FIELD_CONTACT_TYPE); }
            set { SetAttribute(FIELD_CONTACT_TYPE, value); }
        }

        public string Message
        {
            get { return GetAttribute<string>(FIELD_MESSAGE); }
            set { SetAttribute(FIELD_MESSAGE, value); }
        }
    }
}

using Sitecore.Analytics.Model.Framework;
using System;
namespace MySite.Models.Facets
{
    [Serializable]
    public class AdditionalInfoFacet : Facet, IAdditionalInfoFacet
    {
        public const string FACET_NAME = "Company Info";
        private const string FIELD_COMPANY = "Company";
        private const string FIELD_INDUSTRY = "Industry";
        private const string FIELD_SUB_INDUSTRY = "SubIndustry";
        private const string FIELD_REVENUE_RANGE = "RevenueRange";
        private const string FIELD_OPT_IN = "OptIn";
        public AdditionalInfoFacet()
        {
            EnsureAttribute<string>(FIELD_COMPANY);
            EnsureAttribute<string>(FIELD_INDUSTRY);
            EnsureAttribute<string>(FIELD_SUB_INDUSTRY);
            EnsureAttribute<string>(FIELD_REVENUE_RANGE);
            EnsureAttribute<string>(FIELD_OPT_IN);
            EnsureCollection<IAdditionalInfoCommunicationElement>(FACET_NAME);
        }
        public string Company
        {
            get { return GetAttribute<string>(FIELD_COMPANY); }
            set { SetAttribute(FIELD_COMPANY, value); }
        }
        public string Industry
        {
            get { return GetAttribute<string>(FIELD_INDUSTRY); }
            set { SetAttribute(FIELD_INDUSTRY, value); }
        }
        public string SubIndustry
        {
            get { return GetAttribute<string>(FIELD_SUB_INDUSTRY); }
            set { SetAttribute(FIELD_SUB_INDUSTRY, value); }
        }
        public string RevenueRange
        {
            get { return GetAttribute<string>(FIELD_REVENUE_RANGE); }
            set { SetAttribute(FIELD_REVENUE_RANGE, value); }
        }
        public IElementCollection<IAdditionalInfoCommunicationElement> Communications
        {
            get
            {
                return GetCollection<IAdditionalInfoCommunicationElement>(FACET_NAME);
            }
        }
        public bool OptIn
        {
            get { return GetAttribute<bool>(FIELD_OPT_IN); }
            set { SetAttribute(FIELD_OPT_IN, value); }
        }
    }
}

But the following code snippet:

if (registration.ForceOptIn == true || registration.ShowOptIn == false || (registration.ShowOptIn == true && !string.IsNullOrWhiteSpace(registration.OptIn)))
{
    additionalInfo.OptIn = true;
}
else
{
    additionalInfo.OptIn = false;
}

Throws the following error:

> Exception: System.InvalidCastException Message: Unable to cast object
> of type
> 'Sitecore.Analytics.Model.Framework.ModelAttributeMember`1[System.String]'
> to type
> 'Sitecore.Analytics.Model.Framework.IModelAttributeMember`1[System.Boolean]'.
> Source: Sitecore.Analytics.Model    at
> Sitecore.Analytics.Model.Framework.Element.SetAttribute[TValue](String
> name, TValue value)    at
> MySite.Analytics.ContactManagement.RegisterContact(Registration
> registration) in
> C:\SRC\Main\Sitecore\MySite.Analytics\ContactManagement.cs:line 127

To me it look like everywhere I reference OptIn I am using a bool, but this error seems to indicate I am using a string somewhere?

I am running Sitecore 8.1 Update 2

10

In your AdditionalInfoFacet constructor, change this line:

EnsureAttribute<string>(FIELD_OPT_IN);

To this:

EnsureAttribute<bool>(FIELD_OPT_IN);

You need to register your boolean attribute with the correct type.

| improve this answer | |
  • Ok, it is time to go to bed or more coffee. I looked at that code for like an hour trying to find what it was. :-( – eat-sleep-code Oct 13 '16 at 19:33
  • I feel like there could have been a more helpful error message here! – user1069816 Mar 20 '18 at 14:29

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.