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?
Update: Crm Value Accessor Set
Sitecore Value Accessor Set
Mapping
Sitecore Stack Exchange is a question and answer site for developers and end users of the Sitecore CMS and multichannel marketing software. It only takes a minute to sign up.
Sign up to join this communityI'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?
Update: Crm Value Accessor Set
Sitecore Value Accessor Set
Mapping
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.
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();
}
}
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:
After that you need to create Bool To Checkbox Value Reader item based on Bool To Checkbox Value Reader template:
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.