Using Salesforce’s SandboxPostCopy interface to modify email addresses

In Salesforce’s Spring ’16 release, the Run Script After Sandbox Creation and Refresh feature was released. It allows developers to create an Apex class that implements the SandboxPostCopy interface and specify the class on the Sandbox Options screen (pictured in the screenshot at the top of this post).

One use for this is to modify email addresses that are included in records as part of refreshing a partial copy or full sandbox. Salesforce automatically modifies sandbox user email addresses to the format of but doesn’t do so for other types of records (such as leads, contacts, or queues). Leaving these unmodified could end up in undesired email messages being sent to real prospects, customers, or employees.

Recently on GitHub, I found some code published by Steve O’Neal that uses SandboxPostCopy in combination with the Batchable interface to modify sandbox email addresses in a way that can handle large numbers of records. I made a few modifications to Steve’s code, including:

  • changing the batch classes so the test class will work in production orgs (not just sandboxes)
  • adding the group object (type = ‘queue’)
  • making the modified contact/lead/queue email addresses to be in the same format as the user email addresses

I created a sandbox-postcopy repository in GitHub with the updated classes and their XML files. Feel free to fork that repository and/or deploy it to your Salesforce org.

2 thoughts on “Using Salesforce’s SandboxPostCopy interface to modify email addresses

  1. Ed Stapf says:

    TY Rob! Looking to do a mass email update w/masking or appending to the the user’s existing Email account name. We do need to avoid the following however ‘notification emails will be sent to each original email address and verification emails will be sent to each new email address to authorize ‘ while testing in a Test region and post PROD to lower region refresh – we have actual live emails in play. Need to mass update each and every user’s email address.

    Liked by 1 person

Leave a Reply to Ed Stapf Cancel reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s