trigger wen user record is inserted/updated..check if user is active or not.. profile should be standard user..and if both conditions meet..then add user to a particular public group
trigger addintoPublicgroup on User (after insert, after update) {
AddUser.AddToGroups(trigger.newMap.keySet());
}
This class is referenced in the apex trigger Add to Group in order to add users automatically to public groups.
public class AddUser{
@future
public static void AddToGroups(Set<Id> userIds)
{
//Get the groups that the user should be added to
Group g=[select Id from Group Where DeveloperName='Test Group'];
List<User> users=[select id,isActive from user where id=:userinfo.getuserid()];
if(!u.isActive)
{
flag = false;
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error,'Error. User not Active.'));
}
List<GroupMember>listGroupMember =new List<GroupMember>();
// loop the users that have been created
for (User user : users){
GroupMember gm= new GroupMember();
gm.GroupId=g.id;
gm.UserOrGroupId = user.id;
listGroupMember.add(gm);
}
insert listGroupMember;
}
}