Filtering on NetworkScope Returns UnsupportedQueryExpressionException

Free edition only

Symptoms

When trying to use the sfnetworkscope field to filter on a specific community ID, the query works when used alone, but fails when trying to add a second query layer. This can happen, for instance, when using the NOT operator, as such:

NOT @sfnetworkscope="Your Community Id"

The query returns a Something Went Wrong error with the following message under More Information:

{
 "status": 400,
  "message": "The query expression of type BinaryFieldExpression cannot be converted to SOQL/SOSL: @sfnetworkscope="Your Community Id". NetworkScope filters can only be top-level AND expressions",
  "type": "UnsupportedQueryExpressionException",
  "name": "UnsupportedQueryExpressionException",
  ...
}

Cause

This error is due to a SOSL limitation from Salesforce, which prevents the networkscope field from being used outside of top-level AND expressions. When using terms such as NOT, the networkscope query becomes outside of top-level AND expressions.

Resolution

Ensure the sfnetworkscope field is used only as top-level AND expressions. This means that, while you cannot use NOT, you can use @sfnetworkscope=(your first community Id, your second community Id, your third community Id).