You need to sign in to do that
Don't have an account?
Sam Swann
Please help me out in dealing with this.
Thanks!
How to remove a user from a group using Apex
Hi,
I wanted to remove a user if the user's profile is changed or if I make user inactive.
Here is the part of code I am working on, which throws me an error:
EXCEPTION_THROWN [41]|System.ListException: Missing id at index: 0
@future public static void DeteleFromGroups(Set<Id> userIds){ Group g=[SELECT Id, DeveloperName from Group Where DeveloperName='Supervisors']; //Fetching Group List<GroupMember> listGroupMember =new List<GroupMember>(); //Group member instance List<User> users=[Select Id, Name, User.Profile.Name, User.IsActive from user Where Id IN :userIds]; //Fetching user for (User user : users){ GroupMember gm= new GroupMember(); if((user.Profile.Name != 'Supervisor' && user.IsActive) || (user.Profile.Name == 'Supervisor' && user.IsActive == FALSE)){ gm.GroupId=g.id; gm.UserOrGroupId = user.id; listGroupMember.add(gm); } } if(listGroupMember.size() > 0){ delete listGroupMember; //delete listGroupMember[0].UserOrGroupId; } }
Please help me out in dealing with this.
Thanks!
Let us know if this will help you
All Answers
Let us know if this will help you
Awesome. That code works like piece of butter.
Just small change to the query in the If statement after adding GroupMember info:
It's nothing wrong in the code, but just a assignment issue with UserOrGroupId. You're good with this working.
Thank you very much.
Sam