function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shailesh ChaudharyShailesh Chaudhary 

How to create a new public group using Apex?

Is it right way to create public group?

A public group comprises a group of individual users, other groups ,individual roles, or roles with subordinates and all having a common function. You can define sharing rules with the public rules in Salesforce. The public groups can extend sharing rules beyond the role hierarchies. The user logs into Salesforce for creating a new public group and then navigates to   (Credit: AnavClouds Software)

Administer-> Manage Users->Public Groups. 
When we create a new Salesforce user, the user must be assigned to a new public group, depending on the user country. 
Add the user to the right public group with the following steps: 
Create a trigger for the User object and call the method AddUser belonging to the class named AddUsersToPublicGroups in afterInsert. 
Use the Trigger.new context variable to pass the user record. 
For an already existing user record with a new country, it should be added to the newly added public group, removing it from the previous public group along with the country. 
Next, call the updateUser method of the class AddUserstoPublicGroup in afterUpdate and use the Trigger.new and Trigger.old context variables for passing the User Record. 
Create a class AddUsersPublicGroups and write the below code. 

public class AddUsersToPublicGroups {
public static void addUser(list<User> user){
    List<GroupMember> listGroupMember =new List(); 
    for(User u : user){
    String division = u.Division; 
        if(!String.isBlank(division)){
        String groupname ='PublicGroup'+division;
            list<Group> group1 = [select id,name from Group where Name=:groupname and type='regular'];
            GroupMember gm= new GroupMember(); 
            gm.GroupId=group1[0].id;
            gm.UserOrGroupId = u.id;
            listGroupMember.add(gm);
        }
    }
    insert listGroupMember;
}
    public static void updateUser(list oldversion,list newversion){
        List<GroupMember> listGroupMember =new List<GroupMember>();
        list<groupMember> groupm = new list<groupMember>();
        for(user oldu :oldversion){
            for(user newu : newversion){
                String olddivision = oldu.Division;
                String newdivision = newu.Division;
                group group2 = [select id,name from Group where Name=:'PublicGroup'+olddivision and type='regular'];
                if(olddivision!=newdivision){
                    GroupMember gm= new GroupMember(); 
            gm.GroupId=group2.id;
               gm.UserOrGroupId = newu.id;
                    String gid = gm.GroupId;
                    String userid = gm.UserOrGroupId;
               listGroupMember.add(gm);
                    groupm = [select Id from GroupMember where  GroupId=:gid and UserOrGroupId=:userid];
                }
            }
        }
        
try{
         delete groupm;
        }catch(DmlException ex){
         ex.getMessage();
        }
        
adduser(newversion);
    }
}
AbhishekAbhishek (Salesforce Developers) 
try below code to create a group 

Group test = new Group();
test.Name = 'Test Group';
Insert test;


for more information refer https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_group.htm


And the below blog have code snippet too,

https://www.biswajeetsamal.com/blog/send-email-to-a-public-group-using-apex-in-salesforce/

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
RituSharmaRituSharma
https://developer.salesforce.com/forums/ForumsMain?id=9062I000000ISMYQA4