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
BB 

How to return all the accounts that a user has acces to view based on the userID and role

I want to return all the accounts a user has and has access to see  for a specific role otherwise to return only the accounts the he has
I tried something like this but is not working
 System.Debug(UserInfo.getUserRoleId());
        if((UserInfo.getUserRoleId()).equals('BOSS')|| (UserInfo.getUserRoleId()).equals('BOSS 2')){
    return [SELECT id, name, AccountStatus__c, OwnerId,
            FROM Account
            LIMIT:100000];
            
        }else {
           return [SELECT id, name, AccountStatus__c, OwnerId, 
            FROM Account
            WHERE OwnerId =: UserInfo.getUserId() 
            LIMIT:100000]; 
Best Answer chosen by B
Raj VakatiRaj Vakati
try this
 
System.Debug(UserInfo.getUserRoleId());

User uu = [select Id, Name, UserRole.Name  from User where Id =:UserInfo.getUserRoleId()]


        if((uu.UserRole.Name.equals('BOSS')|| (uu.UserRole.Name.equals('BOSS 2'))){
    return [SELECT id, name, AccountStatus__c, OwnerId,
            FROM Account
            LIMIT:100000];
            
        }else {
           return [SELECT id, name, AccountStatus__c, OwnerId, 
            FROM Account
            WHERE OwnerId =: UserInfo.getUserId() 
            LIMIT:100000];

 

All Answers

Raj VakatiRaj Vakati
try this
 
System.Debug(UserInfo.getUserRoleId());

User uu = [select Id, Name, UserRole.Name  from User where Id =:UserInfo.getUserRoleId()]


        if((uu.UserRole.Name.equals('BOSS')|| (uu.UserRole.Name.equals('BOSS 2'))){
    return [SELECT id, name, AccountStatus__c, OwnerId,
            FROM Account
            LIMIT:100000];
            
        }else {
           return [SELECT id, name, AccountStatus__c, OwnerId, 
            FROM Account
            WHERE OwnerId =: UserInfo.getUserId() 
            LIMIT:100000];

 
This was selected as the best answer
BB
That solved my problem