Search code examples
salesforcesalesforce-developer

Does not accept a WITH clause - Salesforce


When using below SOQL query in Salesforce for working with "WITH filteringExpression" referred from https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_with.htm

SELECT Id FROM Lead WITH Name='YYYYY'

I got below error. May I know why I got this error.

ERROR at Row:1:Column:26
Lead does not accept a WITH clause

Solution

  • WITH is a special filter for something else that would be hard to express in SOQL otherwise. It tries to hide a very complex JOIN operation you'd have to make. What you need is old-school WHERE Name = 'YYYYY'.

    WITH is used to effectively retrieve records related to data categories. Categories are bit like picklists but hierarchical. I've never used them outside of Knowledge Base implementations. Imagine having FAQ articles and you want an article about lawnmower care to appear when searching for "Garden Care", "Home & Garden", "Consumer-grade products". That's kind of what that example in documentation tries to convey. Find me articles that are USA-related (which would be a very specific category) but also articles linked to more generic North America.