I am not fortunate enough to work with the Unlimited Edition of Salesforce.com and purchasing a Full Sandbox is a pricy proposition, so when I do a sandbox refresh all I get is an empty shell of our org with no data. Since I always find myself creating accounts and opportunities right away, I decided to build a tool to do it a bit more quickly. The data isn’t fancy, but it is just test data which give me a head start when testing other areas of functionality.
I wanted to make sure that this tool didn’t run on prod (that would not be good) so you will notice the getOnSandbox method which helps figure out whether the current environment is on a sandbox or not. Note, for those of you who have a sandbox on tapp*, when you create a visualforce page it appears to run on c.cs* so checking for the presence of “cs” in the host name should still work for you.
The other trick here is allowing us to deploy this to production. This tool is useless if it it gets wiped out after a refresh, so we want to deploy it to production. That is where Test.isRunningTest() comes in useful. Checking for that will allow us to create the data when we are running a test, allowing the proper code coverage to be attained.
So here is what I have put together. Remember that your environment might be different. I actually updated this code to work on a standard developer environment, but you may want to add other data that gets created; just use this as a base.
When you first visit the page there will be two buttons. Their functions are obvious. There is Create and Delete. The image below show what happens when the create button is first clicked. Notice that it is greyed out so we don’t clicking it twice accidentally.

Once the data has been created, Names and Ids are displayed.

And if you try to access this from a production environment, it won’t work.

Hopefully somebody finds this useful. [Update: be sure to check out the comments to see how Wes Nolte accomplished this in a very cool way.]
The Class
public with sharing class createSandboxDummyData {
private List accountList = new List();
private List opportunityList = new List();
//indicates the number or records to create for EACH Account Record Type
public Integer MAXRECORDS=5;
//these bool's will be used to help render the page
public Boolean dataAdd {get;private set;}
public Boolean dataDelete {get;private set;}
public createSandboxDummyData(){
dataAdd = false;
dataDelete = false;
}
//are we on a sandbox?
public boolean getOnSandbox(){
return System.URL.getSalesforceBaseURL().getHost().contains('cs');
}
//next two methods return the record lists
public List getAccounts(){
return accountList;
}
public List getOpportunities(){
return opportunityList;
}
//Now time for the real work.
public void createData(){
//Make sure we are on a dev server or running a test.
//We DO NOT want to create dummy data on a Prod Server
if( getOnSandbox() || Test.isRunningTest()){
//update the type of action
dataAdd=true;
dataDelete=false;
//Let's Create some Accounts
accountList = new List();
for(Integer i=1;i accountList.add( new Account(Name='Test Account xyz ' + i));
}
insert accountList;
//Great, we have some accounts, how about some opportunities
opportunityList = new List();
//Use this counter to give unique names to opportunities
Integer oppCounter=1;
for(Account a : accountList){
opportunityList.add(
new Opportunity(Name='Test Opportunity xyz '+oppCounter,
AccountId=a.id,
CloseDate=Date.Today().addDays(40+oppCounter),
StageName='Trial Period',
Probability=0.4
)
);
oppCounter++;
}
insert opportunityList;
}
}
//Just in case we don't want all that data that we created,
//we can delete it
public void deleteData(){
if(getOnSandbox() || Test.isRunningTest()){
//update the type of action
dataDelete=true;
dataAdd=false;
//update the lists of accounts and opportunities.
accountList = [select Id, Name from Account where name like '%Test Account xyz%'];
opportunityList = [select Id, Name from Opportunity where name like '%Test Opportunity xyz%'];
delete accountList;
//we don't need to delete the opportunity list here because
//all opps are deleted when the accounts are deleted
}
}
}
The Page (be sure to only give access to admins)
<apex:page sidebar="false" controller="createSandboxDummyData" tabStyle="Account">
<apex:outputText rendered="{!NOT(onSandbox)}" ><br/> Whoa Tiger! This is a production environment. Nothing to see here.</apex:outputText>
<apex:outputPanel rendered="{!onSandbox}">
<apex:form id="theForm">
<apex:pageBlock >
<!-------------------------------------------------- Create and Delete buttons ------------------------------------------>
<apex:actionStatus id="creatingStatus">
<apex:facet name="stop">
<apex:commandButton action="{!createData}" status="creatingStatus" value="Create Dummy Data" disabled="false" reRender="theForm"/>
</apex:facet>
<apex:facet name="start">
<apex:commandButton status="creatingStatus" value="Creating..." disabled="true"/>
</apex:facet>
</apex:actionStatus>
<apex:actionStatus id="deletingStatus">
<apex:facet name="stop">
<apex:commandButton action="{!deleteData}" status="deletingStatus" value="Delete Dummy Data" disabled="false" reRender="theForm"/>
</apex:facet>
<apex:facet name="start">
<apex:commandButton status="creatingStatus" value="Deleting..." disabled="true"/>
</apex:facet>
</apex:actionStatus>
<!---------------------------------------- output for created or deleted accounts and opportunities -------------------------------->
<apex:panelGrid columns="2" width="100%">
<apex:outputPanel >
<br/> <!-----This helps align the tables in the output-->
<apex:pageBlockSection title="{!IF(dataAdd,'Accounts Added','Accounts Deleted')}" rendered="{!dataAdd || dataDelete}">
<div width="100%">
<apex:pageBlockTable value="{!Accounts}" var="a" >
<apex:column value="{!a.Name}" headerValue="Account Name"/>
<apex:column value="{!a.Id}" headerValue="Account Id"/>
</apex:pageBlockTable>
</div>
</apex:pageBlockSection>
</apex:outputPanel>
<apex:outputPanel >
<apex:pageBlockSection title="{!IF(dataAdd,'Opportunities Added','Opportunities Deleted')}" rendered="{!dataAdd || dataDelete}">
<div width="100%">
<apex:pageBlockTable value="{!Opportunities}" var="o" >
<apex:column value="{!o.Name}"/>
<apex:column value="{!o.Id}"/>
</apex:pageBlockTable>
</div>
</apex:pageBlockSection>
</apex:outputPanel>
</apex:panelGrid>
</apex:pageBlock>
</apex:form>
</apex:outputPanel>
</apex:page>
The Test Class
@isTest
private class testCreateSandboxDummyData {
static testMethod void myUnitTest() {
DateTime startTime = DateTime.Now();
//instantiate controller
createSandboxDummyData csdd = new createSandboxDummyData();
List aList = [select Id, Name from Account where name like '%Test Account xyz%' AND CreatedDate >= :startTime];
System.assertEquals(0, aList.size());
List oList = [select Id, Name from Opportunity where name like '%Test Opportunity xyz%'AND CreatedDate >= :startTime];
System.assertEquals(0, oList.size());
//test the create data button
csdd.createData();
aList = [select Id, Name from Account where name like '%Test Account xyz%' AND CreatedDate >= :startTime];
System.assertEquals(csdd.MAXRECORDS , aList.size());
oList = [select Id, Name from Opportunity where name like '%Test Opportunity xyz%'AND CreatedDate >= :startTime];
System.assertEquals(csdd.MAXRECORDS , oList.size());
//test the helper methods
System.AssertEquals(csdd.MAXRECORDS,csdd.getAccounts().size());
System.AssertEquals(csdd.MAXRECORDS,csdd.getOpportunities().size());
//test the funtionality to determine if we are on a sandbox or not
if(System.URL.getSalesforceBaseURL().getHost().subString(0,2).startsWith('cs'))
System.assert(csdd.getOnSandBox());
else
System.assert(!csdd.getOnSandBox());
//test the delete data button
csdd.deleteData();
aList = [select Id, Name from Account where name like '%Test Account xyz%' AND CreatedDate >= :startTime];
System.assertEquals(0 , aList.size());
oList = [select Id, Name from Opportunity where name like '%Test Opportunity xyz%'AND CreatedDate >= :startTime];
System.assertEquals(0 , oList.size());
}
}