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
Joseph KennedyJoseph Kennedy 

How to write a batch class to append emails of contacts in Sandbox

Hi Developer friends, 

I am trying to write a batch class that replaces "@" with "=" and appends "@example.com" to the end of all contact emails as part of my post UAT refresh steps. How could I go about accomplishing this?

Best, 
Joe
Best Answer chosen by Joseph Kennedy
Abdul KhatriAbdul Khatri
Here you go

Batch Class
global class ContactEmailUpdateBatch implements Database.Batchable<sObject>{

   global final String Query;
   
   global ContactEmailUpdateBatch(){
      
       Query='SELECT Email FROM Contact WHERE Email != null';

   }

   global Database.QueryLocator start(Database.BatchableContext BC){
       return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<Contact> scope){                     

        List<Contact> contactsToUpdate = new List<Contact>();              
        for(Contact contact : scope)
        {

            contact.Email = contact.Email.Replace('@','=') + '@example.com';
            contactsToUpdate.add(contact);
        }      
        
        if(!contactsToUpdate.isEmpty())
            update contactsToUpdate;
   }

   global void finish(Database.BatchableContext BC){

   }
}

Test Class
 
@isTest
private class ContactEmailUpdateBatchTest {

    @testSetup 
    static void setup() {
        List<Contact> contacts = new List<Contact>();
        // insert 10 accounts
        for (Integer i=0;i<10;i++) {
            contacts.add(new Contact(firstname='first', lastname='last', email='test' + i + '@test.com'));
        }
        insert contacts;
        
    }
    static testmethod void test() {        
        Test.startTest();
        ContactEmailUpdateBatch uca = new ContactEmailUpdateBatch();
        Id batchId = Database.executeBatch(uca);
        Test.stopTest();
        // after the testing stops, assert records were updated properly
        System.assertEquals(10, [select count() from contact where Email LIKE '%example.com']);
    }
    
}

 

All Answers

Abdul KhatriAbdul Khatri
Here you go

Batch Class
global class ContactEmailUpdateBatch implements Database.Batchable<sObject>{

   global final String Query;
   
   global ContactEmailUpdateBatch(){
      
       Query='SELECT Email FROM Contact WHERE Email != null';

   }

   global Database.QueryLocator start(Database.BatchableContext BC){
       return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<Contact> scope){                     

        List<Contact> contactsToUpdate = new List<Contact>();              
        for(Contact contact : scope)
        {

            contact.Email = contact.Email.Replace('@','=') + '@example.com';
            contactsToUpdate.add(contact);
        }      
        
        if(!contactsToUpdate.isEmpty())
            update contactsToUpdate;
   }

   global void finish(Database.BatchableContext BC){

   }
}

Test Class
 
@isTest
private class ContactEmailUpdateBatchTest {

    @testSetup 
    static void setup() {
        List<Contact> contacts = new List<Contact>();
        // insert 10 accounts
        for (Integer i=0;i<10;i++) {
            contacts.add(new Contact(firstname='first', lastname='last', email='test' + i + '@test.com'));
        }
        insert contacts;
        
    }
    static testmethod void test() {        
        Test.startTest();
        ContactEmailUpdateBatch uca = new ContactEmailUpdateBatch();
        Id batchId = Database.executeBatch(uca);
        Test.stopTest();
        // after the testing stops, assert records were updated properly
        System.assertEquals(10, [select count() from contact where Email LIKE '%example.com']);
    }
    
}

 
This was selected as the best answer
Abdul KhatriAbdul Khatri
Hi. Was this helpful?
Joseph KennedyJoseph Kennedy
Hi Abdul! Please forgive the very slow reply! It is perfect. I am having some trouble running it in my UAT instance but I have had it confirmed many times that the code is solid. Thanks for your gracious help!

Best, 
Joe