Try our conversational search powered by Generative AI!

Make custom meta fields display in Commerce Manager Contact management

Vote:
 

In our project we've added custom meta fields to the Business Foundation contact object.

We initialize the field in an initialization module:

var customerMetadata = DataContext.Current.MetaModel.MetaClasses
    .Cast()
    .First(mc => mc.Name == "Contact");

if(customerMetadata.Fields["MyCustomProperty"] == null)
{
    customerMetadata.CreateMetaField("MyCustomProperty", "My custom property", MetaFieldType.Text, true, "", new AttributeCollection());
}

Then we get and set the property through CustomerContact.Properties:

customerContact.Properties["MyCustomProperty"].Value = "something";

This works fine. The problem is that the custom property doesn't show up on the Contact Management page in Commerce Manager, neither in the information tab or the edit form. I've seen the "Add field to"-checkboxes in Business Foundation in the Administration tab, but i would like to do this in code. Is it possible to do that?

Thanks!

#120914
Apr 28, 2015 8:35
Vote:
 

Did you figure this out?

#139345
Sep 30, 2015 13:02
Vote:
 

Hi Ian,

We ended up doing this manually in Commerce Manager in all environments.

Administration -> System Settings -> Business Foundation -> Contact -> Forms tab -> View Form.

Lars

#139346
Sep 30, 2015 13:11
Vote:
 

After adding the metafields to the metaclass you can add the field to the desired form by code as well.

The function to use is:
FormController.AddMetaPrimitive(string metaClassName, string formName, string fieldName)
(Mediachase.BusinessFoundation.MetaForm.FormController)

The Form names are:

Edit Form -> [MC_BaseForm]
Short Info Form -> [MC_ShortViewForm]
View Form -> [MC_GeneralViewForm]

#144851
Feb 19, 2016 16:41
Vote:
 

If you wish to do it in a more controlled manner, for example deciding where in the form to put the field, its span, border, label etc you can use the FormDocument instead.

var forms = SqlFormDocumentManager.GetFormDocuments(className) will load all forms for a meta class

var theForm = forms.FirstOrDefault(x => x.DocumentType == "[MC_BaseForm]") 

DocumentType can be used to determine which form you wish to edit, and the form object you end up with will give you access to all rows, columns, sections, controls and items.

SqlFormDocumentManager.Save(theForm) will save your changes.

So to add a meta field to a form you'd need to do something like this.

var newField = new FormItem()
        {
            RowIndex = 0, // Will be placed first
            CellIndex = 1,
            RowSpan = 1,
            ColSpan = 1,
            BorderType = 0,
            ShowLabel = true,
            LabelWidth = "120px",
            Uid = Guid.NewGuid().ToString("N")
        };

        newField.Labels.Add(new FormLabel("My label:", "en")); // Add a label
        newField.Control = new FormControl("MetaPrimitive") // Add the control for the field
        {
            Source = "SourceFieldName",
            ReadOnly = false,
            Uid = Guid.NewGuid().ToString("N")
        };

        theForm.FormTable.Rows[0].Cells[0].Sections[0].Control.Items.Add(newField); // Adding the field to the first section, first cell, first row
        SqlFormDocumentManager.Save(theForm);



#146943
Mar 31, 2016 12:38
Vote:
 

Just a note:

var forms = SqlFormDocumentManager.GetFormDocuments(className) 

will return 0 elements, if you still have default form and only starts returning it if there are some modifications in form(at least for Organization in epi comm 13.19)

#226955
Aug 24, 2020 12:30
Vote:
 

Hello,

We recently had a support case related to this area. The following steps will be resolved it. 

        private void CreateMetaField(string metaClassName, string metaFieldName, string friendlyName,
            bool isNullable = true, int maxLength = 255, bool isUnique = false)
        {
            var metaClass = DataContext.Current.GetMetaClass(metaClassName);
            if (metaClass == null)
            {
                return;
            }
            var fieldExists = metaClass.Fields.Contains(metaFieldName);
            if (fieldExists)
            {
                return;
            }
            using (var metaFieldBuilder = new MetaFieldBuilder(metaClass))
            {
                metaFieldBuilder.MetaClass.AccessLevel = AccessLevel.Customization;
                metaFieldBuilder.CreateText(metaFieldName, friendlyName, isNullable, maxLength, isUnique);
                metaFieldBuilder.SaveChanges();
            }

            //Add metafield to form.
            AddMetaFieldToForms(metaClassName, metaFieldName);
        }

        private static void AddMetaFieldToForms(string metaClassName, string metaFieldName)
        {
            string[] formNames = { FormController.BaseFormType, FormController.GeneralViewFormType, FormController.ShortViewFormType };
            foreach (var formName in formNames)
            {
                FormController.AddMetaPrimitive(metaClassName, formName, metaFieldName);
            }
        }

Then you call to CreateMetaField() method where you want like this:

            //Add custom meta field to contact form programmatically.
            CreateMetaField(ContactEntity.ClassName, "ContactTest_Name", "ContactTest_FriendlyName");

The new control with labeled "ContactTest_FriendlyName" will be showed in "Contact" form in CommerceManager.

Giang.Tran

#276043
Mar 10, 2022 3:36
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.