User userRecord = [ SELECT Id,Name FROM User limit 1];
List<String> permissionSetNames =new permissionSetNames();
permissionSetNames.add('PermissionSet1');//replace with your'e permission set name
permissionSetNames.add('PermissionSet2');//same here
List<PermissionSetAssignment> permissionSetAssignments = new List<PermissionSetAssignment>();
List<PermissionSet> allPermissionSets = [SELECT Id, Name FROM PermissionSet WHERE Name IN :permissionSetNames];
System.assertEquals(permissionSetNames.size(), permissionSets.size());
for (PermissionSet eachPermissionSet :allPermissionSets) {
permissionSetAssignments.add(
new PermissionSetAssignment(
AssigneeId = userRecord.Id,
PermissionSetId = eachPermissionSet.Id
)
);
}
insert permissionSetAssignments;
Hope this helps!!
For any kind of assistance on this, please feel free to contact me on [email protected]
do share and comment if you like it