0

I want to add users to a public group based on profile. This is what i have done so far.

trigger newUserTrigger on User (after insert) {
    string name = 'test Apex trigger';
    list<groupMember> groupList = new list<groupMember>();
    list<Group> grp = [select id from Group where name = : name];
    for(User us : trigger.new){
        if((us.profile.name == 'standard user') || (us.profile.name == 'standard platform user')){
            for(group g : grp){
                groupMember gp = new groupMember(groupId = g.Id);
                gp.UserOrGroupId = us.Id;
                groupList.add(gp);
            }
        }    
    }
    if(groupList.size() > 0)
    insert groupList;   
    }

I am not getting any error but the user is not getting added to the group.

Updated code

trigger newUserTrigger on User (after insert, after update) {

string name = 'test_Apex_trigger';
list<groupMember> groupList = new list<groupMember>();
list<Group> grp = [select id from Group where name = : name];
for(User us : trigger.new){
    System.debug('before if '+us.name);
     if(us.profile.name.equalsIgnoreCase('Standard User') || us.profile.name.equalsIgnoreCase('Standard Platform User')){
        system.debug('after if '+us.name);
        for(group g : grp){
            groupMember gp = new groupMember(groupId = g.Id);
            gp.UserOrGroupId = us.Id;
            groupList.add(gp);
        }
    }    
}
if(groupList.size() > 0)
insert groupList;   
}

There are no users in the user object

3
  • Are you sure there is a group called 'testApexGroup' in your org? – Martin Lezer Jul 26 '17 at 7:30
  • yes the label of the group is the same i have mentioned in the code – Mohit Joshi Jul 26 '17 at 7:33
  • It seems like you were asking something else in the starting and after that problem was resolved, you modified the entire question. – Ankita Jul 26 '17 at 11:01
1

The most possible reason is that you never get into this loop:

for(group g : grp)

So no GroupMember records are created. You should make sure that a group with this name exists. Also, I'd recommend using DeveloperName field on group, since it is unique and Name is not.

1

Here is the updated code. Please try this.

trigger newUserTrigger on User (after insert, after update) {

string name = 'test_Apex_trigger';
list<groupMember> groupList = new list<groupMember>();
list<Group> grp = [select id from Group where name = : name];
Map<Id, Profile> profileIdMap = new Map<Id, Profile>([SELECT Id, name from profile]);
for(User us : trigger.new){
    System.debug('before if '+us.name);
     if(profileIdMap.get(us.profileid).name.equalsIgnoreCase('Standard User') || profileIdMap.get(us.profileid).name.equalsIgnoreCase('Standard Platform User')){
        system.debug('after if '+us.name);
        for(group g : grp){
            groupMember gp = new groupMember(groupId = g.Id);
            gp.UserOrGroupId = us.Id;
            groupList.add(gp);
        }
    }    
}
if(groupList.size() > 0)
insert groupList;   
}

Reason: In trigger.new, you cannot have the access to the reference field. You need to query it.

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.