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 to users whose profile have been changed in apex. I want a solution in terms of salesforce best practice

VinayVinay (Salesforce Developers) 
What permission do you want to assign?  Can you elaborate more on this.

Thanks,
Aditi Mohanty 5Aditi Mohanty 5
Any permission set to any user whose profile is changing. No specific one
Need a code for general use, on the basis of salesforce best practice, as soon as possible.
VinayVinay (Salesforce Developers) 
Check below sample 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])
{ 
    PermissionSetAssignment psa = new PermissionSetAssignment (PermissionSetId = '0PSq00000009qDNGAY', AssigneeId = u.Id);
    permissionSetList.add(psa);
}
try{
    upsert permissionSetList;
}
catch(exception e){
}

Check below references.

https://developer.salesforce.com/forums/?id=9060G000000I6XjQAK
https://jenwlee.com/2020/03/10/draft-permission-set-group-assignment-automation/

Thanks,