How to create public Group based on the profiles

Requirement: I have certain profiles For ex:

   Profile 1
   profile 2
   profile 3
   profile 4

How to add these profiles related user to the public group .

Instead of trigger,can I use anonymous window?

  • this question becomes notable question, but I got down Vote Mar 29 '20 at 14:11

Members of a Public Group can be either - (other) Public Group, Roles, Roles & Sub Ordinates or Users.

I see the following possibilities for adding the related users to the Public Group:

  • If your profiles are sharing specific roles then you can add those to the public group
  • Add the users of each of these profiles to the Public Group.

More helpful information

Let me know if you need more help in solving the requirement.


I am afraid I believe this is not possible without the use of an Apex Trigger.

The trigger would fire on the user object and add the record to a public group when the profile equals your examples.

There is an idea to upvote as well: Allow public groups to reference profiles

Edit, I found this previous question that may help if you go down the trigger route.trigger to add active user with standard profile to public group


Below code works for me

   list<groupMember> groupList = new list<groupMember>();
   Group grp = [select id from Group where DeveloperName='new_public_Group']; 
     List<user> userList=[select id,profileId from User where profileId IN ('00e1g000000HhnW','00eE0000000j8T0','00eE00000015tVE','00eE00000015tVF','00eE00000015tVG','00eE00000015tVI','00eE00000015tVJ','00eE00000015tVH','00eE0000000mva9') and IsActive=true];

  for(user u:userList){ 
  groupMember gp = new groupMember(); 
  gp.groupId = grp.Id; gp.UserOrGroupId = u.Id; groupList.add(gp); 
   if (groupList.size()>0)  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.