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 Feb 11 '16 at 9:59
  • have added the code – wizard Feb 11 '16 at 10:15
  • 2
    so what is not working and why are you using future here? – Tushar Sharma Feb 11 '16 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 Feb 11 '16 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 Feb 11 '16 at 11:10
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 Feb 11 '16 at 10:58
  • got it got it..just chckin the workng nw – wizard Feb 11 '16 at 11:00
  • its not adding to group – wizard Feb 11 '16 at 11:04
  • do you give correct profile name. also check in debug if records get inserted or not. – Tushar Sharma Feb 11 '16 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 Feb 11 '16 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.