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
sriram k 15sriram k 15 

When User created then need to assign permission set automatically based on profile.?

Need to achive this scenario by Apex coding. 
AbhinavAbhinav (Salesforce Developers) 
Hi Sriram,

Have you check suggestion on below link?

https://developer.salesforce.com/forums/?id=906F0000000BDYtIAO

Thanks!
Arfiyan Shaikh 7Arfiyan Shaikh 7
Hi Sriram,

You can write a trigger on User object and use below code to assign permission set

        String  PermissionSetName = 'ReadAll';
        List<id> userIds = new List<id>();  // list of id's from the trigger
        
        List<PermissionSetAssignment> permissionSetList = new List<PermissionSetAssignment>();
        PermissionSet readAllpermission = [SELECT Id  ,name FROM PermissionSet where name = :PermissionSetName];
        System.debug('readAllpermission:::'+readAllpermission);
        
        List<string> existingRealAllUserList = new List<string>();
        List<PermissionSetAssignment> FunnelReadAll = [Select AssigneeId FROM PermissionSetAssignment WHERE  PermissionSet.Name = :PermissionSetName];
        
        
        for(PermissionSetAssignment p: FunnelReadAll){
            existingRealAllUserList.add(p.AssigneeId);
         
        }
        System.debug('existingRealAllUserList:::'+existingRealAllUserList);
    
        List<User> userList =[Select id   from User Where IsActive = true AND id IN :userIds];
        for(User s : userList){
            
         if(!existingRealAllUserList.contains(s.id)){
             
            PermissionSetAssignment psa = new PermissionSetAssignment (PermissionSetId = readAllpermission.id, AssigneeId = s.id);
            permissionSetList.add(psa); 
         }
         
        }
    
        try{
        if(permissionSetList != null)
            upsert permissionSetList;
        }catch(exception e){
            system.debug('exception caught' + e);
        }