8

I'm trying to map a Dynamic Boolean to a Sitecore Checkbox.

The raw value read is "True" but i need to convert to 1

Any tips?

raw value

Update: Crm Value Accessor Set

CRM

Sitecore Value Accessor Set

Sitecore

Mapping

enter image description here

2
  • can you post information regarding your configuration/implementation? This might be a field mapping issue
    – josedbaez
    Jan 24, 2018 at 11:49
  • @josedbaez updated the question
    – VeNoMiS
    Jan 24, 2018 at 11:58

2 Answers 2

7

You can use Field Value Transformer for Write on Sitecore Value Accessor Set. This approach allows you to create a single solution not only for CRM and also for all providers where the source value is Boolean and you need to convert it to Sitecore Checkbox type.

Before to run a writer for Sitecore Field Value Writer transformer converts its value to a right format.

  • Change field IsCommentsEnabled to Sitecore checkbox type.

enter image description here

The Reader

public class BoolToSitecoreCheckboxValueReader : IValueReader
{
    public virtual CanReadResult CanRead(object source, DataAccessContext context)
    {
        if (source is bool)
        {
            return CanReadResult.PositiveResult();
        }

        return CanReadResult.NegativeResult();
    }

    public virtual ReadResult Read(object source, DataAccessContext context)
    {
        if (source is bool value)
        {
            return ReadResult.PositiveResult(value ? "1" : "0", DateTime.Now);
        }

        return ReadResult.NegativeResult(DateTime.Now);
    }
}

The Converter

[SupportedIds("[YOUR TEMPLATE ID]")]
public class BoolToSitecoreCheckboxReaderConverter : BaseItemModelConverter<IValueReader>
{
    public BoolToSitecoreCheckboxReaderConverter(IItemModelRepository repository) : base(repository)
    {
    }

    public override IValueReader Convert(ItemModel source)
    {
        return new BoolToSitecoreCheckboxValueReader();
    }
}
  • Create a template where base template is Value Reader.

enter image description here

  • Set Converter Type

enter image description here

  • Add created reader under existed Tenant under the Value Readers folder
6
  • i'm missing some key points. Do i need to create a template like in the previus answer?
    – VeNoMiS
    Jan 25, 2018 at 10:36
  • Sure. Sorry I thought the hint [YOUR TEMPLATE ID] would help.
    – Vlad Shpak
    Jan 25, 2018 at 11:06
  • So just to recap i create a new template (bool to check) that has Value Reader as Base Template. Then i create a new value reader provider based on the previus template (bool to check provider). Setting the converter Type to BoolToSitecoreCheckBoxReaderConverter. Then on Data Access Sitecore i need to add\edit Is Comment Enable and set the field value transformer for write to: bool to check provider.
    – VeNoMiS
    Jan 25, 2018 at 11:13
  • I updated my answer. Do not forget to use your template id as supported id. Yes in Field Value Transformer for Write select created Reader.
    – Vlad Shpak
    Jan 25, 2018 at 11:36
  • i've redo the whole process twice: the ctor of BoolToSitecoreCheckboxReaderConverter get hitted but the convert isn't. i don't get what i'm missing
    – VeNoMiS
    Jan 25, 2018 at 12:06
5

You need to create Value Reader Converter:

public class BoolToCheckboxValueReaderConverter : BaseItemModelConverter<ItemModel, IValueReader>
    {
        private BoolToCheckboxFieldReader _reader = (BoolToCheckboxFieldReader)null;
        private static readonly Guid TemplateId = Guid.Parse("{C3C9E93C-F197-4FBD-820D-DCB565707AF7}");
        public BoolToCheckboxValueReaderConverter(IItemModelRepository repository)
      : base(repository)
        {
            this.SupportedTemplateIds.Add(TemplateId);
        }

        public override bool CanConvert(ItemModel source)
        {
            return true;
        }

        public override IValueReader Convert(ItemModel source)
        {
            if (source == null)
            {
                Sitecore.DataExchange.Context.Logger.Error("Cannot convert null item to value reader. (converter: {0})", (object)this.GetType().FullName);
                return (IValueReader)null;
            }
            if (!this.CanConvert(source))
            {
                Sitecore.DataExchange.Context.Logger.Error("Cannot convert item to value reader. (item: {0}, converter: {1})", (object)source.GetItemId(), (object)this.GetType().FullName);
                return (IValueReader)null;

            }

            if (_reader == null)
                _reader = new BoolToCheckboxFieldReader(ItemModelRepository);
            return (IValueReader)_reader;
        }
    }

BoolToCheckboxFieldReader source code:

public class BoolToCheckboxFieldReader : IBaseValueReader
    {
        public string AttributeName { get; set; }
        public IItemModelRepository Repository { get; private set; }
        public BoolToCheckboxFieldReader(IItemModelRepository repository)
        {
            Repository = repository;
        }

        public CanReadResult CanRead(object source, DataAccessContext context)
        {
            return new CanReadResult()
            {
                CanReadValue = true
            };
        }

        public ReadResult Read(object source, DataAccessContext context)
        {
            object result = (object)null;

            var sourceObject = source as Microsoft.Xrm.Sdk.Entity;
            if (sourceObject != null)
            {
                object value;
                if (sourceObject.Attributes.TryGetValue(AttributeName, out value))
                {

                    if (value is bool)
                    {
                        var searchObject = (bool)value;
                        return new ReadResult(DateTime.UtcNow)
                        {
                            WasValueRead = true,
                            ReadValue = searchObject ? "1" : "0"
                        };
                    }
                }

            }

            return new ReadResult(DateTime.UtcNow)
            {
                WasValueRead = true,
                ReadValue = "0"
            };
        }
    }

IBaseValueReader source code:

public interface IBaseValueReader : IValueReader
    {
        string AttributeName { get; set; }
    }

In Sitecore you need to create your Value Reader template and set to "Base templates" Value Reader OOTB template:

enter image description here

After that you need to create Bool To Checkbox Value Reader item based on Bool To Checkbox Value Reader template:

enter image description here

Under /sitecore/system/Data Exchange/Data Exchange/Data Access/Value Accessor Sets you need to find your Entity Attribute Value Accessor, which you want to convert, and set Value Reader field (Value Access section) to Value Readers/Providers/Dynamics CRM/Bool To Checkbox Value Reader item.

Try to run your batch and check converted value.

1
  • This answer is not wrong, but Vlad's answer is more in the spirit of the product. Andrei's solution involves duplicating code that already exists (the ability to read an attribute value from an entity). Vlad's answer uses a feature designed for this very requirement (it lets you convert the value after it is ready but before it is written).
    – Adam Conn
    Jan 25, 2018 at 3:25

Your Answer

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

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