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
Gwirizanani SinoiaGwirizanani Sinoia 

assign a user/ users multiple Permissionsets based on their profile during creation in apex

Hi,

Could some help with a solution to assign users multiple with Permissionsets based on the license of their profile and type of profile

Thanks in advance 
Raj VakatiRaj Vakati
You have to create a trigger on the User that assign permission sets 

Like belwo code 
 
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);
}


https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_permissionsetassignment.htm

https://gist.github.com/douglascayers/4c33730a5a86d414a7756a785aedf661


https://community.skuid.com/skuid/topics/apex-to-assign-skuid-license-and-permission-set

https://www.simplysfdc.com/2013/06/salesforce-permission-set-report.html
https://support.formtitan.com/topics/Salesforce/Permission_Set_License_Assignment_Object
Gwirizanani SinoiaGwirizanani Sinoia
Hi Raj, thanks for the response. The key word is multiple with Permissionsets not single as demostrated by your code.

Thanks
 
Raj VakatiRaj Vakati
Still you can do it .. You can builfy the code same code will work