Salesforce Query for Phone format

Don’t use SOQL to find or query records using phone numbers. Instead, use SOSL.

SOSL Example:

FIND {7894561237}
IN Phone FIELDS
RETURNING Account(
Name, Phone, Other_Phone__c
)

Sample Apex Code:

String searchQuery = 'FIND {7894561237} IN Phone FIELDS ';
searchQuery += 'RETURNING Account( Name, Phone, Other_Phone__c )';
List < List < sObject > > searchResult = Search.query( 
    searchQuery 
);
List < Account > listAccounts = ( List < Account > )searchResult.get( 0 );
System.debug(
    'Accounts are ' + 
    listAccounts
);

Example for Person Account Fields:

FIND {7894561237}
IN ALL FIELDS
RETURNING Account(
Name, PersonMobilePhone, Other_Phone__c
)

To remove all the formats from Phone Number, use the below link

Leave a Reply