function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aditi Mohanty 5Aditi Mohanty 5 

how to assign permission set to users belonging to some particular profiles, without manually entering id of profile or users using only apex

SwethaSwetha (Salesforce Developers) 
HI Aditi,
The below code can be used to Assign Permission Set to Users
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);
}

You can change the SOQL query as per your requirement.
Related:https://salesforce.stackexchange.com/questions/60668/assigning-permission-set-to-user-in-apex
https://salesforce.stackexchange.com/questions/130174/can-i-assign-permission-set-to-a-profile-instead-of-user

​​​​​​​Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you