4

I'm doing one of the Trails, converting my .NET and Java knowledge over. I'm stumped on a really simple task.

Objective: Return a list of accounts where the BillingState is the same as the state abbreviation passed in.

Here's my code. I keep being told that I have a 'no viable alternative at character "''" ... I have no idea what that means.

public with sharing class AccountUtils {

    public static List<Account> accountsByState (string stateAbbreviation)
    {
        Set<string> states = new Set<string>  {stateAbbreviation};
        List<Account> accounts = [Select Id, Name from Account where account.BillingState in :states];

        return accounts;
    }
}

Also - I tried the BillingState without the account[dot], tried having BillingState = stateAbbreviation. I'm not sure what I'm doing right, and what wrong.

Problem Script

AccountUtils.accountsByState("AL")

Error Message

Line 1, Column 29. No viable alternative at character "''"

4
  • You have not shared the problematic code with us.
    – Adrian Larson
    Commented Jan 17, 2017 at 18:28
  • The line that I run in the debugger is: AccountUtils.accountsByState("AL") And the Execute Anonymous Error I get is "Line 1, Column 29. No viable alternative at character "''" Commented Jan 17, 2017 at 18:29
  • 1
    Might I suggest working through: Mapping .NET Concepts to Force.com (If that isn't the one you are already working on) Commented Jan 17, 2017 at 19:00
  • That was the one I was working on, Daniel. It missed a few concepts. I went through the material a dozen times before asking. Commented Jan 20, 2017 at 15:25

1 Answer 1

5

Make sure you don't try to declare a string wrapped in double quotes. Only single quotes are allowed in Apex.

Fails

String s = "Some Value";

Works

String s = 'Some Value';

It's worth noting from Apex Code: The Basics:

  • Strings have the following features:

    • They must be surrounded by single quotes.
    • The same parsing rules apply as SOQL strings.
    • String field values can never be empty (only null).
    • String field values are always trimmed (the same semantics as Oracle and the AppExchange API). However, script-level String values can be empty or have leading or trailing spaces.
    • The same escape sequences are currently used as for SOQL strings.
5
  • After reading this and thinking about it, I played around with the debugger call. I changed it from "AL" to 'AL', and added a ; However, now I'm told that the method doesn't exist. Method does not exist or incorrect signature: AccountUtils.accountsByState(String) Commented Jan 17, 2017 at 18:35
  • Did the AccountUtils class actually compile? Are you sure your save operation succeeded? Normally you would see that if you forgot to mark the method as static, but that is not the case in what you posted.
    – Adrian Larson
    Commented Jan 17, 2017 at 18:36
  • 1
    AHHHH I didn't realize that saving was effectively compiling (paradigm difference). Thank you. I saved it, then it worked fine. Commented Jan 17, 2017 at 18:38
  • Welcome to SFSE. :)
    – Adrian Larson
    Commented Jan 17, 2017 at 18:40
  • 1
    Glad it's working now. Here's an error unrelated tip: There;s no need to convert the passed parameter to a set and use "in" in the query. [Select Id, Name from Account where account.BillingState = :stateAbbreviation] will work fine and you can get rid of the Set variable.
    – Jochen
    Commented Jan 17, 2017 at 20:10

You must log in to answer this question.

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