Assigning permission set to user in apex
Turns out it's very easy:
PermissionSetAssignment psa = new PermissionSetAssignment
(PermissionSetId = myPermissionSetId, AssigneeId = myAssigneeId);
insert psa;
List<PermissionSetAssignment> permissionSetList = new List<PermissionSetAssignment>();
for (User u : [SELECT ID,UserRole.Name,Profile.Name,IsActive FROM User WHERE UserRole.Name LIKE '%manager%' AND Profile.Name != 'Operations Manager' AND IsActive = true]){ // Add fields as per your requirement...
PermissionSetAssignment psa = new PermissionSetAssignment (PermissionSetId = '0PSq00000009qDNGAY', AssigneeId = u.Id);
permissionSetList.add(psa);
}
try{
upsert permissionSetList;
}catch(exception e){
system.debug('exception caught' + e);
}
Apex code to Assign Permission Set to Users Created this . Change the query as per requirement and use it , works always