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
Shastry k jShastry k j 

Do not allow creation of account if the Logged user is not assigned to “Create Account” on Permissionsetassignment

AnkaiahAnkaiah (Salesforce Developers) 
Hi Shastry,

By default user have create permission on profile level??

try with below code. Replace
trigger checkpermissionset on Account (before insert) {
    
  id userid = UserInfo.getUserid();
    
  PermissionSetAssignment assignid =[ SELECT AssigneeId FROM PermissionSetAssignment WHERE PermissionSet.Name = 'Create_Account'];
    
    for(account acc :trigger.new){
        if(assignid.AssigneeId != userid){
            
            acc.adderror(' Logged user is not assigned to "Create Account" on Permissionsetassignment');
            
        }
    }

}



Thanks!
CharuDuttCharuDutt
Hii Shastry
Try Below Code
trigger checkpermissionset on Account (before insert) {
RestrictAccountCreationHelper.RestrictUser(Trigger.New);
}

######################################################################

public class RestrictAccountCreationHelper{
public static Void RestrictUser(list<Account>newAccount){

id userid = UserInfo.getUserid();
    
  list<PermissionSetAssignment> assignedPermissionSet =[ SELECT Id FROM PermissionSetAssignment WHERE AssigneeId = :Userinfo.getUserId() AND PermissionSet.Name = 'Create_Account'];
    
    for(account acc :newAccount){
        if(assignedPermissionSet.IsEmpty()){
            
            acc.adderror(' Logged user is not assigned to "Create Account" on Permissionsetassignment');
            
        }
    }

}
}
Please Mark It As Best Asnwer If It Helps
Thank You!