AI OnAI Off
CustomerContext.Current.GetContactById(customerId);
You can add new metafields to the contact object via an initialization module or a migration step. I prefer doing it in a migration step. Here's the reference for it: https://docs.developers.optimizely.com/commerce/v14.0.0-commerce-cloud/docs/metafield-class
FYI it is best practice to check if the field already exists before attempting to create it.
The class name for the contact object is "Contact".
Using SiteInitializationModule is correct. We created a MetaDataHelper to extend our existing table columns.
var contactMetaClass = MetaDataHelpers.GetOrCreateMetaClass(ContactEntity.ClassName);
public static Mediachase.BusinessFoundation.Data.Meta.Management.MetaClass GetOrCreateMetaClass(string metaClassName, string metaClassFriendlyName = null)
{
var metaClass = DataContext.Current.MetaModel.MetaClasses
.Cast<Mediachase.BusinessFoundation.Data.Meta.Management.MetaClass>()
.FirstOrDefault(mc => mc.Name == metaClassName) ?? CreateMetaClass(metaClassName, metaClassFriendlyName ?? metaClassName);
return metaClass;
}
private static Mediachase.BusinessFoundation.Data.Meta.Management.MetaClass CreateMetaClass(string metaClassName, string metaClassFriendlyName)
{
var metaClass = default(Mediachase.BusinessFoundation.Data.Meta.Management.MetaClass);
using (Mediachase.BusinessFoundation.Data.Meta.Management.MetaClassManagerEditScope scope = DataContext.Current.MetaModel.BeginEdit())
{
metaClass = DataContext.Current.MetaModel.CreateMetaClass(metaClassName, metaClassFriendlyName);
scope.SaveChanges();
}
return metaClass;
}
Lastly, You would add the field to the metaObject of Contact as desired -
var fieldsMetaData = new List<FieldMetaData>()
{
new FieldMetaData()
{
Name = BusinessFoundation.Gender,
FriendlyName = BusinessFoundation.Gender,
Type = MetaFieldType.Text,
StringLength = 30
}
}
AddFields(contactMetaClass, fieldsMetaData);
private void AddFields(MetaClass metaClass, List<FieldMetaData> fieldsMetaData)
{
if (metaClass == null || !fieldsMetaData.SafeAny())
{
return;
}
using (var builder = new MetaFieldBuilder(metaClass))
{
foreach (var fieldMetaData in fieldsMetaData)
{
MetaDataHelpers.CreateMetaField(builder, metaClass, fieldMetaData);
}
builder.SaveChanges();
}
}
I can see in the below database that all entries with the `MetaClassId` of 2 are properties that are included in the `Contact` class.
I have two questions: