I want to remove Accounts from List to which user doesn't have access.
Here is my code. However in the debug logs, I can see all the accounts, that is none of them are removed.
query += QUERY_BASE + FROM_BASE + wherClause + criteria + order + limitStr;
returnAccounts= Database.query(query);
//START: Account sharing check:-Fetch all the records where user has access
Set<Id> setAccIds = new Set<Id>();
for(Account a:returnAccounts){
setAccIds.add(a.Id);
}
List<AccountShare> lstAccountShares = [Select AccountId from AccountShare where UserOrGroupId=:UserInfo.getUserId()];
System.debug(LoggingLevel.Error,'MVK lstAccountShares--->'+lstAccountShares);
if(lstAccountShares!=null && lstAccountShares.size() > 0){
Set<Id> setAccountShares = new Set<Id>();
for(AccountShare ashare:lstAccountShares){
setAccountShares.add(ashare.AccountId);
}
System.debug(LoggingLevel.Error,'MVK setAccountShares--->'+setAccountShares);
if(setAccIds.size()>0){
for(Integer i=0;i<returnAccounts.size();i++){
if(!setAccountShares.contains(returnAccounts.get(i).Id)){
//System.debug(LoggingLevel.Error,'MVK setAccountShares--->'+returnAccounts.get(i).Id);
returnAccounts.remove(i);
}
}
system.debug('MVK returnAccounts--->'+returnAccounts);
}
}
else{
returnAccounts.clear(); // If user has access to none of the records then return empty
}