Skip to content

Parse error with SOSL binding vars #44

Closed

Description

@adangel

While testing the examples from https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_variables.htm I found two cases, which are not yet supported by apex-parser:

class BindWithIncludes {
    void example() {
        Account A = new Account(Name='xxx');
        insert A;

        // A bind with INCLUDES clause
        Account B = [SELECT Id FROM Account WHERE :A.TYPE INCLUDES ('Customer - Direct; Customer - Channel')]; // <--- Parse error
    }
}

This gives the following parse error:

Syntax error at 7:50: mismatched input ':' expecting {'for', 'update', 'with', 'using', 'where', 'order', 'limit', 'group', 'all', 'offset', ']', ','}
Syntax error at 7:67: missing ';' at '('
Syntax error at 7:108: extraneous input ']' expecting ';'
class BindWithDivision {
    public void soslQuery() {
        // A SOSL query with binds in all possible clauses

        String myString1 = 'aaa';
        String myString2 = 'bbb';
        Integer myInt3 = 11;
        String myString4 = 'ccc';
        Integer myInt5 = 22;

        List<List<SObject>> searchList = [FIND :myString1 IN ALL FIELDS
                                          RETURNING
                                             Account (Id, Name WHERE Name LIKE :myString2
                                                      LIMIT :myInt3),
                                             Contact,
                                             Opportunity,
                                             Lead
                                          WITH DIVISION =:myString4 // <--- parse error
                                          LIMIT :myInt5];
    }
}

This gives the following parse error:

Syntax error at 18:57: mismatched input ':' expecting StringLiteral
line 19:48 mismatched input ':' expecting {'after', 'before', 'get', 'inherited', 'instanceof', 'set', 'sharing', 'switch', 'transient', 'trigger', 'when', 'with', 'without', 'system', 'user', 'select', 'count', 'from', 'as', 'using', 'scope', 'where', 'order', 'by', 'limit', 'and', 'or', 'not', 'avg', 'count_distinct', 'min', 'max', 'sum', 'typeof', 'end', 'then', 'like', 'in', 'includes', 'excludes', 'asc', 'desc', 'nulls', 'first', 'last', 'group', 'all', 'rows', 'view', 'having', 'rollup', 'tolabel', 'offset', 'data', 'category', 'at', 'above', 'below', 'above_or_below', 'security_enforced', 'system_mode', 'user_mode', 'reference', 'cube', 'format', 'tracking', 'viewstat', 'custom', 'standard', 'distance', 'geolocation', 'calendar_month', 'calendar_quarter', 'calendar_year', 'day_in_month', 'day_in_week', 'day_in_year', 'day_only', 'fiscal_month', 'fiscal_quarter', 'fiscal_year', 'hour_in_day', 'week_in_month', 'week_in_year', 'converttimezone', 'yesterday', 'today', 'tomorrow', 'last_week', 'this_week', 'next_week', 'last_month', 'this_month', 'next_month', 'last_90_days', 'next_90_days', 'last_n_days', 'next_n_days', 'n_days_ago', 'next_n_weeks', 'last_n_weeks', 'n_weeks_ago', 'next_n_months', 'last_n_months', 'n_months_ago', 'this_quarter', 'last_quarter', 'next_quarter', 'next_n_quarters', 'last_n_quarters', 'n_quarters_ago', 'this_year', 'last_year', 'next_year', 'next_n_years', 'last_n_years', 'n_years_ago', 'this_fiscal_quarter', 'last_fiscal_quarter', 'next_fiscal_quarter', 'next_n_fiscal_quarters', 'last_n_fiscal_quarters', 'n_fiscal_quarters_ago', 'this_fiscal_year', 'last_fiscal_year', 'next_fiscal_year', 'next_n_fiscal_years', 'last_n_fiscal_years', 'n_fiscal_years_ago', IntegralCurrencyLiteral, 'find', 'email', 'name', 'phone', 'sidebar', 'fields', 'metadata', 'pricebookid', 'network', 'snippet', 'target_length', 'division', 'returning', 'listview', '[', '.', '<', Identifier}
Syntax error at 19:48: mismatched input ':' expecting {'after', 'before', 'get', 'inherited', 'instanceof', 'set', 'sharing', 'switch', 'transient', 'trigger', 'when', 'with', 'without', 'system', 'user', 'select', 'count', 'from', 'as', 'using', 'scope', 'where', 'order', 'by', 'limit', 'and', 'or', 'not', 'avg', 'count_distinct', 'min', 'max', 'sum', 'typeof', 'end', 'then', 'like', 'in', 'includes', 'excludes', 'asc', 'desc', 'nulls', 'first', 'last', 'group', 'all', 'rows', 'view', 'having', 'rollup', 'tolabel', 'offset', 'data', 'category', 'at', 'above', 'below', 'above_or_below', 'security_enforced', 'system_mode', 'user_mode', 'reference', 'cube', 'format', 'tracking', 'viewstat', 'custom', 'standard', 'distance', 'geolocation', 'calendar_month', 'calendar_quarter', 'calendar_year', 'day_in_month', 'day_in_week', 'day_in_year', 'day_only', 'fiscal_month', 'fiscal_quarter', 'fiscal_year', 'hour_in_day', 'week_in_month', 'week_in_year', 'converttimezone', 'yesterday', 'today', 'tomorrow', 'last_week', 'this_week', 'next_week', 'last_month', 'this_month', 'next_month', 'last_90_days', 'next_90_days', 'last_n_days', 'next_n_days', 'n_days_ago', 'next_n_weeks', 'last_n_weeks', 'n_weeks_ago', 'next_n_months', 'last_n_months', 'n_months_ago', 'this_quarter', 'last_quarter', 'next_quarter', 'next_n_quarters', 'last_n_quarters', 'n_quarters_ago', 'this_year', 'last_year', 'next_year', 'next_n_years', 'last_n_years', 'n_years_ago', 'this_fiscal_quarter', 'last_fiscal_quarter', 'next_fiscal_quarter', 'next_n_fiscal_quarters', 'last_n_fiscal_quarters', 'n_fiscal_quarters_ago', 'this_fiscal_year', 'last_fiscal_year', 'next_fiscal_year', 'next_n_fiscal_years', 'last_n_fiscal_years', 'n_fiscal_years_ago', IntegralCurrencyLiteral, 'find', 'email', 'name', 'phone', 'sidebar', 'fields', 'metadata', 'pricebookid', 'network', 'snippet', 'target_length', 'division', 'returning', 'listview', '[', '.', '<', Identifier}

Activity

  1. added a commit that references this issue on Jun 27, 2024
  2. kjonescertinia commented on Apr 22, 2026

    @kjonescertinia
    Contributor

    Thanks for raising this @adangel, and sorry it's taken so long to get to.

    We tested both reported examples against a Salesforce org using sf apex run and landed a partial fix in #89. Summary:

    Case 2 (SOSL WITH DIVISION = :myString4) — genuine parser gap. Fixed in #89 by extending soslWithClause so WITH DIVISION accepts boundExpression in addition to StringLiteral.

    Case 1 (SOQL WHERE :A.TYPE INCLUDES (...)) — turns out this isn't valid Apex. The compiler rejects it both inline and via Database.queryWithBinds (System.QueryException: unexpected token: ':'). Semantically it would reduce to a comparison between two constants, so it's unlikely to be the intended pattern. The example on the Apex Variables in SOQL/SOSL docs page appears to be a broken snippet — bind-on-RHS (INCLUDES (:c)) is the pattern that actually works in Apex, and that already parses today via the existing value rule.

    While we were testing, we also verified that WITH NETWORK|PRICEBOOKID|METADATA = :bind are all rejected by the Apex compiler (only StringLiteral is accepted in those positions), so the DIVISION fix stays narrowly scoped.

    #89 will partially resolve this — we'll close when it merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions