I would like to automatically add and remove user from a public group.
When they got assigned to a specific profile, they should be added to this group and when the profile has been changed to anything else, they should get moved away from the group.
I made myself a code but due to the fact, that in groupmembers, the profileid is not supported, i am kinda lost here. And in the group are always more then 1 person, and only one specific person should be removed.
is it possible to get the id of the user which caused the trigger to run?
Maybe you could help me?
here is the trigger so far:
trigger AddToPublicGroup on User (after insert, after update) { List<GroupMember> GMlist = new List<GroupMember>();
for (User a : trigger.new){
if (a.profileid != '00eD0000001QPRa') {
GroupMember GL = [Select Id from GroupMember where GroupId = '00GL0000001AMGt'];
delete GL;
}
else
if (a.profileid == '00eD0000001QPRa') {
GroupMember GM = new GroupMember();
GM.GroupId = '00GL0000001AMGt';
GM.UserOrGroupId = a.Id;
GMList.add(GM) ;
insert GMList;
}}
}