Generating Random Test Data for Salesforce Sandboxes and Scratch Orgs Using Python
- Architect
- Jan 24
- 3 min read

As a Salesforce Developer or Administrator, you’ll often need to work with Salesforce Sandboxes and Scratch Orgs. However, these environments don’t always include the data required to test custom code or functionality. When faced with this challenge, you typically have two options:
Manually populate the data using Excel and load it into Salesforce.
Write an anonymous Apex script to create the required test data.
Both options can be time-consuming and may lack flexibility. Is there an easier, more dynamic solution?
Yes, there is!
In this article, we’ll explore how to use Python to generate random test data and upload it directly into Salesforce. This method offers a streamlined and customizable approach to populating data in your org.
Tools Required
To get started, you’ll need two powerful Python libraries:
Faker: A library for generating fake data (e.g., names, addresses, phone numbers, etc.).
Simple Salesforce: A library for connecting to and interacting with Salesforce.
Install both libraries in your Python project using the following command:
pip install faker simple-salesforce
Setting Up Your Python Project
Step 1: Configure Salesforce Credentials
Create a new file called sf_config.py and include your Salesforce credentials:
demo_u = "your_username"
demo_pw = "your_password"
demo_token = "your_security_token"
Note: Replace "your_username", "your_password", and "your_security_token" with your actual Salesforce credentials.
Step 2: Create the Data Loader Script
Create another file called sample_data_loader.py with the following code:
from faker import Faker
from simple_salesforce import Salesforce
fake = Faker()
def generate_fake_accounts_with_contacts(num_records):
"""
Generate fake accounts and their associated contacts.
Args:
num_records (int): Number of accounts to generate, each with contacts.
Returns:
list: A list of dictionaries representing accounts with their contacts.
"""
accounts_with_contacts = []
for _ in range(num_records):
# Generate fake account data
account = {
"Name": fake.company(),
"Phone": fake.phone_number(),
"BillingStreet": fake.street_address(),
"BillingCity": fake.city(),
"BillingState": fake.state(),
"BillingPostalCode": fake.zipcode(),
"Description": f"Credit Card: {fake.credit_card_number()}, Driver's License: {fake.license_plate()}, Email: {fake.email()}"
}
# Generate contacts associated with this account
num_contacts = fake.random_int(min=1, max=5) # Each account has 1-5 contacts
contacts = []
for _ in range(num_contacts):
contact = {
"FirstName": fake.first_name(),
"LastName": fake.last_name(),
"Email": fake.email(),
"Phone": fake.phone_number(),
"MailingStreet": fake.street_address(),
"MailingCity": fake.city(),
"MailingState": fake.state(),
"MailingPostalCode": fake.zipcode(),
"Description": f"SSN: {fake.ssn()}, Driver's License: {fake.license_plate()}, Additional Info: {fake.text()}"
}
contacts.append(contact)
accounts_with_contacts.append({"account": account, "contacts": contacts})
return accounts_with_contacts
def connect_to_salesforce(username, password, security_token):
"""
Connect to Salesforce using simple-salesforce.
"""
sf = Salesforce(username=username, password=password, security_token=security_token)
return sf
def upload_accounts_and_contacts_to_salesforce(sf, accounts_with_contacts):
"""
Upload generated accounts and their associated contacts to Salesforce.
Args:
sf (Salesforce): Authenticated Salesforce object.
accounts_with_contacts (list): List of accounts and their contacts.
"""
for item in accounts_with_contacts:
account = item["account"]
contacts = item["contacts"]
# Create the account
account_id = sf.Account.create(account)["id"]
# Create each contact associated with this account
for contact in contacts:
contact["AccountId"] = account_id
sf.Contact.create(contact)
print(f"Uploaded {len(accounts_with_contacts)} accounts and their contacts to Salesforce.")
# Example usage:
if __name__ == "__main__":
from sf_config import demo_token, demo_u, demo_pw
username = demo_u
password = demo_pw
security_token = demo_token
# Connect to Salesforce
sf = connect_to_salesforce(username, password, security_token)
# Generate 10 fake accounts and contacts
accounts_with_contacts = generate_fake_accounts_with_contacts(10)
# Upload accounts and contacts to Salesforce
upload_accounts_and_contacts_to_salesforce(sf, accounts_with_contacts)
Running the Script
Once you’ve set up the files, follow these steps:
Open your project in VS Code (or any other IDE).
Run the sample_data_loader.py file using Python:
python sample_data_loader.py
The script will generate random accounts and contacts and upload them to your Salesforce org.

Key Benefits
Automation: Eliminate manual data entry or writing Apex scripts for test data.
Flexibility: Easily customize the type and structure of generated data.
Scalability: Generate hundreds or thousands of test records in seconds.
Need Help? Contact Us for Support and Customization
If you need assistance setting up this solution, tailoring it to your specific Salesforce requirements, or building more advanced automation, we’re here to help! Our team of experts specializes in Salesforce development, administration, and customization. Don’t hesitate to reach out to us for support, personalized guidance, or to explore how we can help you optimize your Salesforce workflows. Let’s make your Salesforce experience seamless and efficient!
Comments