I am trying to query a list of objects using a value from the account object being inserted. When the query executes it is returning a row count of 0. However, if I run this same query using the execute anonymous in the dev tools it comes back with results or if I run the SOQL straight up it also works. However, within the trigger it just sits there an mocks me.

Here is the trigger code:

trigger update_county on Account (before insert, before update) {

    for(Account account : Trigger.new) {

        String zip = account.BillingPostalCode;
        System.debug('New account name = ' + account.Name + ' Zip Code = ' + zip);

        County__c county = [Select Id, OwnerId, IsDeleted, Name, CreatedDate, CreatedById, LastModifiedDate, LastModifiedById, SystemModstamp, LastActivityDate, Zip_Code__c, geolocation__Latitude__s, geolocation__Longitude__s, geolocation__c, city__c, state__c, Zip_Class__c FROM County__c WHERE Zip_Code__c = :zip LIMIT 1];
        if (county != null) {
            System.debug('Found county!!!!');

            System.debug('County is: ' + county.Name);
            if (county != null)
                account.County__c = county.Id;
            System.debug('Failed to find county!!!!');


  • 1
    What is that system.debug telling you?
    – EricSSH
    Feb 24, 2015 at 18:11

2 Answers 2


If a query returns zero rows, you'll get an exception this way. Furthermore, your code isn't bulkified. What you're looking for is an Aggregate-Query-Update pattern, as follows:

trigger lookupCounty on Account (before insert, before update) {
    Map<String, Id> countyCodes = new Map<String, Id>();
    for(Account record: Trigger.new) {
        countyCodes.put(record.BillingPostalCode, null);
    for(County__c record:[SELECT Zip__c FROM County__c WHERE Zip__c IN :countyCodes.keySet()]) {
        countyCodes.put(record.Zip__c, record.Id);
    for(Account record: Trigger.new) {
        record.County__c = countyCodes.get(record.BillingPostalCode);

Make sure that the postal code matches the county's zip character for character. For example, 12345 won't match 12345-6789. If that's a concern, you might need to do some pre-processing on the postal codes to make them uniform.


When there are no obvious coding issues (which is the case in your example), it is kind of difficult to guess why the data is not returned. Couple of hints for you to consider to troubleshoot.

  1. Security. Is trigger being executed in the context of the user who has access to this object and data? May be consider making this OWD = Public Read?

  2. There are no spaces or anything when value is inserted into the Account Postal Code?

  3. Because you are using Limit 1 in the query, sfdc should throw an error if there is no exact one record to return. You are saying, it returns zero records, which is little confusing.

  4. Try displaying all country records to debug outside of the for loop and see if there are any country records being retrieved.

  • 1. No, triggers run in system context. 2. Or five-digit versus ten-digit codes, etc. 3. The limit 1 wouldn't cause the exception, it's assigning a query result to a variable when zero or more than 1 rows are returned. That's why we typically use collections for queries. 4. Queries always work in system context.
    – sfdcfox
    Feb 24, 2015 at 20:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .