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
Benedict Yeo 6Benedict Yeo 6 

Cannot get result from search

Hi, I've a problem with the following code.. I can get the o.AccountId correctly, but whenever I run the SOQL query, the result returned in an empty set.. What am I doing wrong?
 
public class prefillFields {
    
    public static void applyAddress(Order o) {
        System.debug('o.AccountId: ' + o.AccountId);
        String accountId = o.AccountId;
        
        List<Account> accounts = Database.query('SELECT id, Name, BillingCity, Department_NS__c, ReportingCountry_NS__c FROM Account WHERE Id = :accountId');
            //[SELECT id, Name, BillingCity, Department_NS__c, ReportingCountry_NS__c FROM Account]; // WHERE Id = :o.AccountId];
        if(accounts.size() > 0) {
            System.debug('Gotten address..?');
            System.debug('ID: ' + accounts[0].id);
            System.debug('City: ' + accounts[0].billingCity);
            
            for (Account a : accounts) {
                String accountName = a.Name;
                
                String billingCity = a.BillingCity;
                System.Debug('Billing City: ' + billingCity);
            }
        } else {
            System.debug('List is empty!');
        }
    }
}

 
Balaji Chowdary GarapatiBalaji Chowdary Garapati
@Benedict Yeo 6:

 Do you have read access to that particular record?? (Assuming you have read access @ object level), to verify try adding without sharing clause to your class declaration and see if it works

Thanks,
balaji