+ Start a Discussion
Test Test 143Test Test 143 

how to create a new public group using apex(Anonymous window in salesforce ?

Hi All, 

I have list of users and need to add them to a public group using apex.

help me on this.
Best Answer chosen by Test Test 143
RituSharmaRituSharma
In some orgs, Salesforce pushes some default users like Automated Process, Data.com Clean, System etc. These users are not visible through UI but they do exist in the org and returned by SOQL query(you may check in Developer console). You cannot add them to the groups. Also, guest users can't be added to the groups. So update your query to filter out the relevant users(based on role, profile or some other logical parameter).

All Answers

RituSharmaRituSharma
Code for creating a group
----------------------------------------
Group groupObj = new Group();
groupObj.Name = 'Test Group';
insert groupObj;

Code for adding a member to the group
------------------------------------------------------
GroupMember gmObj= new GroupMember();              
gmObj.GroupId=groupObj.id;             
gmObj.UserOrGroupId = userObj.Id;    
insert gmObj;

Incase of multiple users, loop on the users and add then create separate member for each and add to the list.
Test Test 143Test Test 143
Thanks Ritu Sharma,

I am getting below error for below code: "
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: unknown (invalid user or group: 0050o00000WeunQ): [unknown]"

Code:-
======
Group groupObj = new Group();
groupObj.Name = 'Test Group123';
insert groupObj;
list<GroupMember> gmt=new list<GroupMember>();
List<user> users=[select id from user];
for(user u:users){
GroupMember gmObj= new GroupMember();              
gmObj.GroupId=groupObj.id;             
gmObj.UserOrGroupId =u.id;    
gmt.add(gmObj);
}
insert gmt; 

Thanks!
RituSharmaRituSharma
Update the query to filter out the inactive users.
Test Test 143Test Test 143
Thanks Ritu Sharama.

I have tried with below code, But getting same error. Please help me.

Group groupObj = new Group();
groupObj.Name = 'Test Group123';
insert groupObj;
list<GroupMember> gmt=new list<GroupMember>();
List<user> users=[select id from user where isActive = true];
for(user u:users){
GroupMember gmObj= new GroupMember();              
gmObj.GroupId=groupObj.id;             
gmObj.UserOrGroupId =u.id;    
gmt.add(gmObj);
}
insert gmt;
RituSharmaRituSharma
In some orgs, Salesforce pushes some default users like Automated Process, Data.com Clean, System etc. These users are not visible through UI but they do exist in the org and returned by SOQL query(you may check in Developer console). You cannot add them to the groups. Also, guest users can't be added to the groups. So update your query to filter out the relevant users(based on role, profile or some other logical parameter).
This was selected as the best answer
Test Test 143Test Test 143
Thanks Ritu Sharma.

My problem has been solved with your sugestions.
Sprutiraj Panda 7Sprutiraj Panda 7
Hi 
on the above code The line no 6 was showing the below Error 

Illegal assignment from List<User> to List<user>