0

We wont be able to add "Permission Set" to all the users (or with a particular userTypes). Like we cannot add "Permission Set" to a UserType - "Guest" or a community User.

I'm trying to assign a "Permission Set" to all the users in a particular Group and I'm using a batch job for the same.

I'm using the below method to get all the userIds for a given groupId.

public static Set<Id> GetUserIdsFromGroup(Id groupId) {
      Set<Id> result=new Set<Id>();
      String userType = Schema.SObjectType.User.getKeyPrefix();
      String groupType = Schema.SObjectType.Group.getKeyPrefix();

      for (GroupMember m : [Select Id, UserOrGroupId From GroupMember Where GroupId = :groupId])
      {
          if (((String)m.UserOrGroupId).startsWith(userType))
          {
              result.add(m.UserOrGroupId);
          }
          else if (((String)m.UserOrGroupId).startsWith(groupType))
          {
            List<Group> groupWithRole=[Select Id, RelatedId From Group where Id=:m.UserOrGroupId];

              if(groupWithRole != null && groupWithRole.size() > 0 && groupWithRole[0].RelatedId != null) {
                  String roleId = groupWithRole[0].RelatedId;
                  List<User> roleUsers= [Select Id from User where UserRole.Id=:roleId];
                  for(User tempUser : roleUsers){
                      result.add(tempUser.ID);
                  }
              }
              else {
                  result.addAll(GetUserIdsFromGroup(m.UserOrGroupId));
              }
          }
      }

      return result;
    }

I cannot add "Permission Set" to each user separately as I might cross the 100 limit, and if I'm trying to add "Permission Set" to all the users at once it breaks as I've some users to whom we cant give "Permission Set".

Can't complete this operation because it would exceed the number of apps allowed by the user license.

List<PermissionSetAssignment> learnersPermission = new List<PermissionSetAssignment>();
        for(String userId:userIds) {
          PermissionSetAssignment learnerPermission = new PermissionSetAssignment();
          learnerPermission.PermissionSetId = 'LearnerPermissionSetID';
          learnerPermission.AssigneeId = userId;

          learnersPermission.add(learnerPermission);
        }

        insert learnersPermission;

Any suggestion or help would be greatly appreciated, thanks.

  • 1
    Have you tried using Database.insert(learnersPermission, false) to allow "good" records to complete successfully? – David Reed Apr 14 '18 at 11:24
  • Great! Moved to an answer. – David Reed Apr 16 '18 at 13:08
1

The DML-related methods under Database take two parameters: an Sobject list and a Boolean allOrNone parameter. Passing false for the second parameter allows each object insert/update/delete operation to fail or succeed individually. You'll get back a List<Database.SaveResult> that defines the results of each object's DML, in the same order as the list you pass in.

So here, instead of insert learnersPermission (which is inherently allOrNone = true), do

Database.insert(learnersPermission, false);

You can inspect the returned List<Database.SaveResult> if you want to take any action on the failures, but if all you want is to let the good records go through, this change should be enough.

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.