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
Jugbeer BholaJugbeer Bhola 

SOQL Encrypted Fields

A request has been made to create a custom lookup to the Contact sObject with a lightning web component.  The Name, FirstName and LastName are encrypted.

An error is returned of ‘Field 'Name' cannot be filtered in a query call’

Is there any work around or trick to this?  

 
Best Answer chosen by Jugbeer Bhola
Jugbeer BholaJugbeer Bhola

I did see that post that you referred too but it did not appear to work.  The answer is 'No' there are no work arounds for encryption and querying encrypted fields with SOQL. You must use SOSL. If you are testing SOSL and expect to return a value you will be disappointed.  You must use Test.setFixedSearchResults to return a value back.
 

@AuraEnabled(cacheable=true)
  public static list<sObject> fetchLookupData(
    string searchKey,
    string sObjectApiName
  ) {

    if (Test.isRunningTest()) {
      List<Contact> TheContact = [SELECT Id FROM Contact LIMIT 1];
      Id[] fixedSearchResults = new Id[1];
      fixedSearchResults[0] = TheContact[0].Id;
      Test.setFixedSearchResults(fixedSearchResults);
    }

    List<sObject> returnList = new List<sObject>();

    String searchStr1 = '*' + searchKey + '*';
    String searchQuery =
      'FIND \'' +
      searchStr1 +
      '\' IN ALL FIELDS RETURNING ' +
      sObjectApiName +
      ' (id, Name)';

    List<List<sObject>> searchList = search.query(searchquery);
    Contact[] searchContacts = (Contact[]) searchList[0];
    for (sObject obj : searchContacts) {
      returnList.add(obj);
    }
    return returnList;
  }

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

If the field is Field is encrypted then we cannot use the field in Where Clause.

Please find the below  similar question (https://developer.salesforce.com/forums/?id=9062I000000ILXtQAO#:~:text=Encrypted%20fields%20that%20use%20the,GROUP%20BY%20clause) where the solution is answered.

If this solution helps, Please mark it as best answer.

Thanks,
 
Jugbeer BholaJugbeer Bhola

I did see that post that you referred too but it did not appear to work.  The answer is 'No' there are no work arounds for encryption and querying encrypted fields with SOQL. You must use SOSL. If you are testing SOSL and expect to return a value you will be disappointed.  You must use Test.setFixedSearchResults to return a value back.
 

@AuraEnabled(cacheable=true)
  public static list<sObject> fetchLookupData(
    string searchKey,
    string sObjectApiName
  ) {

    if (Test.isRunningTest()) {
      List<Contact> TheContact = [SELECT Id FROM Contact LIMIT 1];
      Id[] fixedSearchResults = new Id[1];
      fixedSearchResults[0] = TheContact[0].Id;
      Test.setFixedSearchResults(fixedSearchResults);
    }

    List<sObject> returnList = new List<sObject>();

    String searchStr1 = '*' + searchKey + '*';
    String searchQuery =
      'FIND \'' +
      searchStr1 +
      '\' IN ALL FIELDS RETURNING ' +
      sObjectApiName +
      ' (id, Name)';

    List<List<sObject>> searchList = search.query(searchquery);
    Contact[] searchContacts = (Contact[]) searchList[0];
    for (sObject obj : searchContacts) {
      returnList.add(obj);
    }
    return returnList;
  }
This was selected as the best answer