function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abhinav Sharma.Abhinav Sharma. 

Filter Encrypted field in SOQL Query

I have a requirement where i have to filter the SOQL on the basis of encrypted field.
Is there any possiblity to achieve this as by default encrypted field 'Account_Number__c' can not be filtered in a query call.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Abhinav,

Greetings to you!

According to Salesforce doc: https://help.salesforce.com/articleView?id=security_pe_considerations_general.htm&type=5 (https://help.salesforce.com/articleView?id=security_pe_considerations_general.htm&type=5)

Encrypted fields that use the probabilistic encryption scheme can’t be used with the following SOQL and SOSL clauses and functions:
  • Aggregate functions such as MAX(), MIN(), and COUNT_DISTINCT()
  • WHERE clause
  • GROUP BY clause
  • ORDER BY clause

Consider whether you can replace a WHERE clause in a SOQL query with a FIND query in SOSL.

One of the most common problems encountered is the restriction around including encrypted fields in the where clause of a SOQL statement. A SOSL find statement can sometimes be used in lieu of a SOQL statement.
 
List<Contact> contacts = [Select Id From Contact Where Email = :emailaddress];

The above query will fail on installation (or at runtime if this is dynamic SOQL) if the the Contact email field is encrypted. However the Query can be replaced with the following SOSL statement:
 
List<List<SObject> > contacts = [FIND :emailaddress IN ALL FIELDS Returning Contact (Id, FirstName, Last Name]

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Jairaj SinghJairaj Singh
Use "View Encrypted Data" permission if not referring shield encryption