32

Is it possible? Are there any limitations to consider? I have a use case where if a certain relationship is maybe, we would assign a permission set that grants access to a knowledge base data category.

2 Answers 2

51

Turns out it's very easy:

PermissionSetAssignment psa = new PermissionSetAssignment
(PermissionSetId = myPermissionSetId, AssigneeId = myAssigneeId);
insert psa;      
7
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

1
  • 3
    Can you add some explanation of why this code answers the question?
    – Adrian Larson
    Commented Oct 2, 2018 at 22:17

You must log in to answer this question.

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