Missing Metadata Attribute in MetaField

Vote:
 

Hi

We updated from 14.5 to latest 14.15.2.

we get

System.NullReferenceException: Object reference not set to an instance of an object.

   at EPiServer.Commerce.UI.Admin.BusinessFoundation.Internal.MetaManagement.CheckboxBooleanManage.GetData(MetaField metaField)
   at EPiServer.Commerce.UI.Admin.BusinessFoundation.Internal.DataManagement.BusinessFoundationDataExtension.ToPropertyDetailViewModel(EntityObjectProperty property, MetaField metaField)
   at EPiServer.Commerce.UI.Admin.Customers.ContactExtensions.ToOrganizationDetailViewModel(Organization organization)
   at System.Linq.Enumerable.SelectEnumerableIterator`2.ToList()
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at EPiServer.Commerce.UI.Admin.Customers.Internal.CustomerService.SearchOrganizations(String query, String sortField, SortingElementType sortType, Int32 startIndex, Int32 recordsToRetrieve, Guid excludedOrgId)

   at lambda_method404(Closure , Object , Object[] )


What is Label here: Probably it does not exist in our db?? why not use ContainsKey here?



Database looks like this:



no contact, no orgs, error above

#311248
Edited, Oct 22, 2023 11:56
Vote:
 

Tried this but hade problem with SaveChanges() probably because we didnt have any XML in XSAttribute column

The problem was that for metaField type “CheckboxBoolean” there was no attribute with the “Label” key. I decompiled the Commerce.UI.Admin DLL and noticed in GetData it was looking for the “Label” key. This was null in my case. I wrote the following code to traverse the metaFields and add the key to that field type.

 

//MY CODE

using (MetaClassManagerEditScope scope = dataContext.MetaModel.BeginEdit())

              {

                  foreach (MetaClass metaClassLoop in dataContext.MetaModel.MetaClasses)

                  {

                      foreach (MetaField field in metaClassLoop.Fields)

                      {

                          if (!field.Attributes.ContainsKey("MaxLength"))

                          {

                              field.Attributes.Add(McDataTypeAttribute.StringMaxLength, 40000);

                          }

 

                          if (field.TypeName == "CheckboxBoolean" && !field.Attributes.ContainsKey("Label"))

                          {

                              field.Attributes.Add(McDataTypeAttribute.BooleanLabel, field.Name);

                          }

                      }

                  }

 

                  scope.SaveChanges();

              }
#311249
Edited, Oct 22, 2023 11:58
Vote:
 

As we see in the table, we have NULL in XSAttribute, so we needed to add some XML to that field, we accomplished that with this MigrationStep:

using System;
using EPiServer.DataAbstraction.Migration;
using EPiServer.ServiceLocation;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;

namespace Gosso.Bug.Workarounds
{
    /// <summary>
    /// missing "Label" in database, this can be removed after run once.
    /// </summary>
    public class AddMissingAttributesMigrationStep : MigrationStep
    {
        public override void AddChanges()
        {
            string connectionString = ServiceLocator.Current.GetInstance<IConfiguration>().GetConnectionString("EPiServerDB");

            bool runOnce = false;

            string query = @"
            SELECT [XSAttributes]
            FROM [X.Commerce].[dbo].[mcmd_MetaField]
            WHERE typename = 'CheckboxBoolean' AND metaclassid = 3 AND [XSAttributes] IS NULL;
            ";

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();

                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            runOnce = true;
                        }
                        else
                        {
                            Console.WriteLine("No rows found with NULL XSAttributes values.");
                        }
                    }
                }
            }


            if (runOnce)
            {


                query = @"
            UPDATE [X.Commerce].[dbo].[mcmd_MetaField]
            SET XSAttributes = '<?xml version=""1.0"" encoding=""utf-8""?>
            <AttributeCollection>
              <Attr>
                <Name>Label</Name>
                <Type>System.String, System.Private.CoreLib</Type>
                <Value><string>True</string></Value>
              </Attr>
              <Attr>
                <Name>MaxLength</Name>
                <Type>System.Int32, System.Private.CoreLib</Type>
                <Value>
                  <int>40000</int>
                </Value>
              </Attr>
            </AttributeCollection>'
            WHERE typename = 'CheckboxBoolean' AND metaclassid = 3";

                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        int rowsAffected = command.ExecuteNonQuery();
                    }
                }
            }
        }
    }
}
#311250
Oct 22, 2023 12:00
Vote:
 

No that didnt work, as we dont know the names in DXP.

we emailed support to run these two scripts: (to add the missing xmls in XSAttribute)

SQL Script 1:

UPDATE [CHANGETHIS.Commerce].dbo.mcmd_MetaField SET XSAttributes = '<?xml version="1.0" encoding="utf-8"?><AttributeCollection><Attr><Name>Label</Name><Type>System.String, System.Private.CoreLib</Type><Value><string>True</string></Value></Attr><Attr><Name>MaxLength</Name><Type>System.Int32, System.Private.CoreLib</Type><Value><int>40000</int></Value></Attr></AttributeCollection>'

WHERE typename = 'CheckboxBoolean' AND metaclassid = 3 and XSAttributes is Null

 

SQL Script 2:

UPDATE [CHANGETHIS.Commerce].dbo.mcmd_MetaField SET XSAttributes = '<?xml version="1.0" encoding="utf-8"?><AttributeCollection><Attr><Name>MaxLength</Name><Type>System.Int32, System.Private.CoreLib</Type><Value><int>40000</int></Value></Attr></AttributeCollection>'
WHERE typename = 'Text' and XSAttributes is Null

#312887
Nov 21, 2023 12:22
Vote:
 

That should be fixed in latest version. 14.15.3, or if you wait a little bit, 14.15.4

#312888
Nov 21, 2023 14:14
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.