ApexSensei

Drills Debugging

Fix a query that finds no rows

Assigning a query to one record throws when nothing matches.

Your task

Fix phoneFor so it returns null when no Account has that name, instead of throwing.

public class AS_1603269E2B24_AccountPhones {
    public static String phoneFor(String accountName) {
        Account match = [
            SELECT Phone
            FROM Account
            WHERE Name = :accountName
            LIMIT 1
        ];
        return match.Phone;
    }
}

Requirements

  1. returnsThePhoneOfAMatch should pass
  2. returnsNullWhenNoAccountMatches should pass
  3. returnsNullForABlankName should pass
  4. returnsNullWhenTheMatchHasNoPhone should pass

Sign in to open this drill