# Check whether the customer group exist by name

**URL:** <https://forum.litium.com/t/check-whether-the-customer-group-exist-by-name/2043>\
**Category:** Questions\
**Tags:** litium-7\
**Created:** [May 5, 2021, 6:23am UTC](https://forum.litium.com/t/check-whether-the-customer-group-exist-by-name/2043 "2021-05-05T06:23:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vishnu\_kj](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/vishnu_kj/32/685_2.png) [@vishnu\_kj](https://forum.litium.com/u/vishnu_kj)\
**Post date:** [May 5, 2021, 6:23am UTC](https://forum.litium.com/t/check-whether-the-customer-group-exist-by-name/2043/1 "2021-05-05T06:23:55Z")

</div>

I have created a customer group named “FriendsAndFamily” like this  
 ![image](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/ca8ee72679a1970d490bb99968c7829be26ea21f.png)

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

When I tried using this, i get null

```auto
 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]_

---

<div class="post-metadata">

**Author:** ![Ericsj11](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/ericsj11/32/899_2.png) [@Ericsj11](https://forum.litium.com/u/Ericsj11)\
**Post date:** [May 5, 2021, 6:46am UTC](https://forum.litium.com/t/check-whether-the-customer-group-exist-by-name/2043/2 "2021-05-05T06:46:37Z")

</div>

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(" ", "");
                groupService.Update(friendsAndFamilyGroup);
            }
        }
```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/12eddac3b8b661de1db86af10ac8151b571e6fb9.png) [@system](https://forum.litium.com/u/system)\
**Post date:** [June 2, 2021, 6:47am UTC](https://forum.litium.com/t/check-whether-the-customer-group-exist-by-name/2043/3 "2021-06-02T06:47:13Z")

</div>

Closed on June 2, 2021, 6:47am UTC

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