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
Nagarajan CNagarajan C 

this query is working fine get the account result in Query Editor and Anonymous Window but not getting any result from apex class

 List<Account> accountList = [SELECT Id,Name From Account];     System.debug('result'+accountList);  this query is working fine get the account result  in Query Editor and Anonymous Window but not  getting any result from apex class can you please help on this
SandhyaSandhya (Salesforce Developers) 
Hi,

Try below code to print.
 
for(Account ac:accountList)
{
System.debug('result' +ac);
}

Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
                                             
Best Regards
Sandhya
 
Ravi Dutt SharmaRavi Dutt Sharma
Is it a with sharing or without sharing class? If nothing is mentioned in the signature of the class, just add without sharing and check the output.

public without sharing class YourClassName
Nagarajan CNagarajan C
Hi Sharma,

I tried without sharing  also but not able to get the account list.
Nagarajan CNagarajan C
public without sharing class AccountProcessor {
    
    @TestVisible
    static void countContacts(){
        List<Account> accountList = [SELECT Id,Name From Account];
        System.debug('Result'+accountList);
              
        if(accountList.size()>0){
        Account account= accountList.get(0);
        account.Number_of_Contacts__c = String.valueOf('1');
        update account;
    }
        
    }

}
Nagarajan CNagarajan C
Above is my sample code 
Ravi Dutt SharmaRavi Dutt Sharma
Who is the caller of this class?
Raj VakatiRaj Vakati
What is the OWD for Account ?? 

If its a Private and User dnt have view all access on account object then it will not return all the data  .. Only you can able to get the data that is having access


Login in as Runing user and create a record and then see you are getting the result or not 

 
public without sharing class AccountProcessor {
    
    @TestVisible
    static void countContacts(){
		
		try{
 List<Account> accountLis = new List<Account> ();
         accountList = [SELECT Id,Name From Account];
        System.debug('Result'+accountList);
              
        if(accountList.size()>0){
        Account account= accountList.get(0);
        account.Number_of_Contacts__c = String.valueOf('1');
        update account;
    }
		}catch(Exception e){
		}
        
    }

}