1

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;
}
}
5
  • 2
    So what have you tried so far?
    – Matt Lacey
    Commented Feb 11, 2016 at 9:59
  • have added the code
    – wizard
    Commented Feb 11, 2016 at 10:15
  • 2
    so what is not working and why are you using future here? Commented Feb 11, 2016 at 10:40
  • @tushar i want it for active user and users with "standard user" profile.. i dunno how to give these 2 conditions
    – wizard
    Commented Feb 11, 2016 at 10:43
  • Doing a check for profile type makes me wince a bit. It might be better to think about your roles and design your group around that, if possible.
    – powlo
    Commented Feb 11, 2016 at 11:10

1 Answer 1

3

As i am not sure why you are using future method but you can try this

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'];

        Set<Id> GrpuserIdSet = new Set<Id>();

        for( GroupMember gm : [select id, memberId from GroupMember  WHERE groupId =: g.Id ]) {
            //GrpuserIdSet.add(memberId);       
            userIds.remove(memberId);
        }

        // add your profile name here or store them in Customsetting and use them
        List<User> users=[select id from user where id IN: userIds AND isActive = true and (Profile.Name = 'System Admin' OR Profile.name = 'System Admin']; 

        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);   
        } 

    if(listGroupMember.size() > 0)
        insert listGroupMember;

    }
}
9
  • its givin this error Compile Error: expecting a right parentheses, found ']' at line 9 column 149
    – wizard
    Commented Feb 11, 2016 at 10:58
  • got it got it..just chckin the workng nw
    – wizard
    Commented Feb 11, 2016 at 11:00
  • its not adding to group
    – wizard
    Commented Feb 11, 2016 at 11:04
  • do you give correct profile name. also check in debug if records get inserted or not. Commented Feb 11, 2016 at 11:10
  • ya working fine nw..n if we have to check that the user already exists in the group and should not get added..wat condition will v apply?
    – wizard
    Commented Feb 11, 2016 at 11:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .