+ Start a Discussion
Yogesh BiyaniYogesh Biyani 

SOQL help (find contacts with emails with numbers before the @ )

Here are some examples of fake contacts in our database which we would like to clean. 
User-added image
Vinod Chokkula 3Vinod Chokkula 3
Use a batch class to idenfy and delete the fake contacts

global class ContactsDelBatch implements Database.Batchable<sObject>
{

    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT Id,Name,email from Contact';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Contact> contacts)
    {
       List<Contact> contactListToDelete = new List<Contact>();
        for ( Contact c : contacts)
        {

           String[] tempStrings = c.email.split('@');
           if(tempStrings[0].isNumeric()){
             contactListToDelete.add(c); 
           }
        }
        if(contactListToDelete.size()>0){
              delete contactListToDelete;
         }
    }  
    global void finish(Database.BatchableContext BC)
    {
    }
}

Save this class and run from anonymous class
Id batchJobId = Database.executeBatch(new ContactsDelBatch(), 200);
Yogesh BiyaniYogesh Biyani
Hello Vinod,

Thanks for the detailed reply and I sure this will be useful. However,  I was thinking of using SOQL because I want to manually check in the developer console before deleting the same.

Yogesh
Vinod Chokkula 3Vinod Chokkula 3

Create a formula field Is_Number_Email__c = ISNUMBER(LEFT(Email,Find("@", Email)-1)) 

Use the formula field to filter the results
SELECT Id,Name,Email,Is_Number_Email__c from Contact where Is_Number_Email__c=true