0

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
}
  • Are you sure that your execution flow reaches to the point where you are removing the items from the list? Are all conditions satisfied to get there? – Jayant Das Apr 10 at 15:03
  • Yupp.. I debugged it. Logs even shows the remove(Integer) statement for every record. – mvk1991 Apr 10 at 17:50
  • do you think there is some issue when I am iterating the same list and removing from the same list? I think should try separating these two things. – mvk1991 Apr 10 at 17:51
  • There could be possibly an issue of skipping records I think. Take a look as mentioned in this answer and see if that helps. – Jayant Das Apr 10 at 17:54
  • Thanks Jayant, that worked.. seems like it was skipping records :) – mvk1991 Apr 11 at 5:48
4

It's much easier to create a tiny with sharing helper class and allow the platform to handle visibility calculations for you. It could look something like this:

public with sharing class QueryHelper {
    public static List<sObject> query(String q) {
        return Database.query(q);
    }
}

Then, in your main class, do

query += QUERY_BASE + FROM_BASE + wherClause + criteria + order + limitStr;
returnAccounts = (List<Account>)QueryHelper.query(query);

That obviates all the complexity of manually querying the share table and calculating visibility, something the platform is already very good at doing for you.

  • Thanks David, this seems to be a good solution. However, I would like to have the edit access to the accounts, right now in Share Table I can see RowCause as ImplicitParent and Access Level = Read, still I am able to edit the account records, and the records which are not present in the share table, because of view All permission on profile, I am able to read but not edit. that's why I was looking for the custom solution. – mvk1991 Apr 11 at 5:39
  • Also the class in which is getting called and defined is with sharing. so not sure why is it returning me all records? – mvk1991 Apr 11 at 5:46
  • @mvk1991 It's returning all records because you have View All. with sharing applies the relevant sharing regime. If you have View All permission, sharing rules are ignored by the system because you have explicit permission to view every record. It sounds like you've adopted an approach that is kind of at odds with the system's built-in functionality. You may want to ask a slightly broader question to get some feedback on more idiomatic ways to handle what you want to do. – David Reed Apr 11 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.