London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

Add a new CustomerGroup dynamically

Vote:
 

Hi, using Commerce is there a way to add to the CustomerGroup Dynamically with code?

I can see where you can set and get a specific customer's CustomerGroup but I cant see how you can add a new CustomerGroup item via code into the CustomerGroup list.

Jon

#82375
Mar 11, 2014 12:43
Vote:
 

Hi,

This is how I did it in versions prior to 7.5, have not had to do this in 7.5 so don't know if it still works there.

----------

var groupName = "MyNewCustomerGroup";
foreach (MetaFieldType fieldType in DataContext.Current.MetaModel.RegisteredTypes)
{
    if (fieldType.Name == "ContactGroup")
    {
        if (!fieldType.EnumItems.Any(item => string.Compare(item.Name, groupName, true) == 0))
        {
            Mediachase.BusinessFoundation.Data.Meta.Management.MetaEnum.AddItem(fieldType, groupName, fieldType.EnumItems.Last().OrderId + 1);
        }
        break;
    }
}

----------

 

/Viktor

#82393
Edited, Mar 11, 2014 16:11
Vote:
 

The code Vickor posted is correct and should be working, but I suggest a little change to make it more effective:

var metaFieldType = Mediachase.BusinessFoundation.Data.DataContext.Current.MetaModel.RegisteredTypes["ContactGroup"];

You now can use metaFieldType directly without loop.

Regards.

/Q

 

 

#82419
Mar 12, 2014 8:03
* 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.