Building a Free Salesforce Org Scanner with Python
As a senior Salesforce administrator, one of my tasks often involves auditing and assessing large Salesforce organizations. While Salesforce provides numerous tools for managing orgs, there are times when an automated script can save significant time and effort. In this article, I will walk you through the process of building a free Salesforce organization scanner using Python. This tool will help you gather critical information about your org's metadata structure, user roles, and more.
Prerequisites
Before we get started, ensure that you have the following:
- Python 3.x: You can download it from python.org.
- Salesforce CLI (SFDX): Install SFDX to interact with Salesforce orgs via command line.
- Bash or Command Prompt: For running scripts and commands.
Setting Up the Environment
First, let’s create a directory for our project:
mkdir salesforce_org_scanner
cd salesforce_org_scanner
Next, install simple-salesforce, which is a Python wrapper for Salesforce APIs:
pip install simple-salesforce
Understanding SOQL Queries
Salesforce Object Query Language (SOQL) is used to query data from Salesforce. Here are some key queries we will use in our script.
Querying Users
To fetch all users and their roles, you can use the following SOQL:
SELECT Id, Username, Email, ProfileId FROM User
Querying Profiles
To get information about user profiles:
SELECT Id, Name, Description FROM Profile
Querying Custom Metadata Types
If your org uses custom metadata types (CMTs), you can query them like this:
SELECT Id, Label, Value FROM MyCustomMetadataType__mdt
Replace MyCustomMetadataType__mdt with the actual name of your CMT.
Writing the Python Script
Now that we have our environment set up, let’s write a basic script to connect to Salesforce and fetch data using SOQL queries.
- Import necessary libraries:
import simple_salesforce
from datetime import datetime
- Set up authentication and connection details:
sf = simple_salesforce.Salesforce(
username='your_username',
password='your_password',
security_token='your_security_token'
)
- Fetch user data:
def fetch_users(sf):
users = sf.query('SELECT Id, Username, Email, ProfileId FROM User')
for record in users['records']:
print(f"User: {record['Username']}, Email: {record['Email']}, ProfileId: {record['ProfileId']}")
- Fetch profile data:
def fetch_profiles(sf):
profiles = sf.query('SELECT Id, Name, Description FROM Profile')
for record in profiles['records']:
print(f"Profile: {record['Name']}, Description: {record['Description']}")
- Main function to run the script:
if __name__ == '__main__':
start_time = datetime.now()
print("Starting Salesforce Org Scanner at", start_time)
# Fetch users and profiles
fetch_users(sf)
fetch_profiles(sf)
end_time = datetime.now()
print("Finished at", end_time, "Total time taken:", end_time - start_time)
- Run the script:
python salesforce_org_scanner.py
Enhancing the Script
To make our scanner more robust and useful, we can add more features such as querying custom objects, retrieving setup components like Apex classes, or generating reports.
Querying Custom Objects
SELECT Id, Name, Label FROM CustomObject__c
Retrieving Apex Classes
from simple_salesforce import Salesforce
sf = Salesforce(username='your_username', password='your_password', security_token='your_security_token')
def fetch_apex_classes(sf):
apex_classes = sf.query('SELECT FullName, Body FROM ApexClass')
for record in apex_classes['records']:
print(f"Apex Class: {record['FullName']}, Body: {record['Body'][:100]}...")
if __name__ == '__main__':
# Fetch users and profiles
fetch_users(sf)
fetch_profiles(sf)
# Fetch Apex classes
fetch_apex_classes(sf)
Conclusion
By now, you should have a good understanding of how to build a basic Salesforce org scanner using Python. This script can be further enhanced by adding more SOQL queries, handling exceptions, and integrating with other tools like CSV files or databases.
Free Org Scanner
For those looking for an even simpler way to audit their Salesforce orgs, consider trying the free Org Scanner at https://orgscanner.dev/?utm_source=devto&utm_medium=content&utm_campaign=free_blitz. It provides a user-friendly interface to gather and visualize critical org data.
Happy coding!
Top comments (0)