Data seeding in Salesforce: maintain the same auto number values across different orgs
Data seeding between organizations in Salesforce is a common practice for several reasons. It facilitates data consistency, ensures testing environments mirror production environments and aids in data migration and backup processes. However, one critical aspect often challenges: dealing with auto number fields during data seeding.
Autonumber fields in Salesforce are unique identifiers that automatically increment with each new record. The main issue arises when we want to maintain the same auto number values across different orgs.
Converting the auto number fields to text will help preserve the auto number values during data seeding. This conversion ensures that the unique identifiers remain consistent across various Salesforce orgs.
Despite its robustness, Salesforce doesn't provide a direct API to change the 'Name' field from auto number to text and vice versa. To fill the gap, A python script is designed to convert the field types, enabling the seamless conversion of the auto number field to text. It's a practical solution for Salesforce administrators and developers who often face the challenge of data seeding across multiple environments and spend numerous hours of manual effort.
from playwright.sync_api import Page, expect, sync_playwright
from simple_salesforce import Salesforce, SalesforceLogin
from urllib.parse import urlparse, urljoin
import pandas as pd
import json
def run(playwright):
username = '**'
password = '**'
security_token = '**'
domain = 'login' # For production, use 'login' - For sandbox, use 'test'
sf = Salesforce(username=username, password=password, security_token=security_token, domain=domain)
# Print the domain URL
domain_url = sf.base_url
custom_object = 'Automation_Test__c'
query = f"SELECT DurableId,QualifiedApiName from EntityDefinition where QualifiedApiName = '{custom_object}'"
result = pd.DataFrame(sf.query_all(query)['records'])
result = result.drop('attributes', axis=1)
# axis=1 means delete the column
# axis=0 means delete the rows
print(result)
browser = playwright.chromium.launch(headless=True)
context = browser.new_context()
page = context.new_page()
page.goto(domain_url, wait_until="networkidle")
page.wait_for_selector("input[name='username']")
page.fill("input[name='username']", "***")
page.wait_for_selector("input[name='pw']")
page.fill("input[name='pw']", "***")
page.click("input[name='Login']")
page.wait_for_load_state("networkidle")
current_url = page.url
parsed_url = urlparse(current_url)
base_url = f"{parsed_url.scheme}://{parsed_url.netloc}/"
for i, row in result.iterrows():
object_id = row['DurableId']
relative_path = f"/p/setup/custent/CustomEntityNameFieldEdit?id={object_id}&retURL={object_id}"
full_url = urljoin(base_url, relative_path)
print(full_url)
page1 = context.new_page()
page1.goto(full_url, wait_until="networkidle")
page1.locator('#AutoNo').select_option('Text')
page1.click("input[name='save']")
page1.wait_for_load_state("networkidle")
page1.wait_for_load_state()
with sync_playwright() as playwright:
run(playwright)
To run the above Python script, the below libraries need to be installed using the following commands:
$ pip install pytest-playwright
$ pip install pandas
$ pip install simple_salesforce
Install the required browsers:
$ playwright install
Once all the scripts are installed, execute the Python script, which will automatically convert the name field of the given object. Modify the EntityDefinition
query to utilize this script for multiple objects. The script can also be extended to support the reverse operation.
While Salesforce doesn't provide direct functionality to convert auto number fields to text, solutions like this script can bridge this gap, ensuring seamless data seeding processes without much manual effort.