Check whether the customer group exist by name

I have created a customer group named “FriendsAndFamily” like this

I need to get that group by its name via code.

When I tried using this, i get null

 var friendsAndFamilyGroup = groupService.Get<Group>("FriendsAndFamily");

The method accepts only string Id. But we cannot specify the id of the group by our own, instead we can specify name only.

How can i find customer group by name?

Litium version: [7.6.1]

You could do it with a DataService, but that should not be used if it’s a flow for a customer on the site.

My approach to this would be to add ID based on the name with the help of a GroupCreated/GroupUpdated event. Something like this. (Sudo code)

            eventBroker.Subscribe<GroupUpdated>(x => UpdateGroupId(x.SystemId));

       using (_securityContextService.ActAsSystem("UpdateGroupId"))
            var friendsAndFamilyGroup = groupService.Get<Group>(systemId).MakeWritableClone();

            var name = friendsAndFamilyGroup.Fields.GetValue<string>("_name", culture);

            if (!friendsAndFamilyGroup.Id.Equals(name))
                friendsAndFamilyGroup.Id = name.Replace(" ", "");
1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.