You need to sign in to do that
Don't have an account?
Test 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.
I have list of users and need to add them to a public group using apex.
help me on this.
All Answers
----------------------------------------
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.
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!
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;
My problem has been solved with your sugestions.
on the above code The line no 6 was showing the below Error
Illegal assignment from List<User> to List<user>