Ever wondered how to seamlessly sync data between Salesforce orgs without duplicating or overloading storage? With Salesforce Connect, you can access external data in real-time! In this guide, we’ll walk you through setting up two Salesforce orgs—Source Org and Target Org—to sync Account records effortlessly. Using External Data Sources, Batch Apex, and a scheduled sync job, you’ll achieve live, automated data integration that keeps your orgs connected and efficient. Ready to transform your workflows? Let’s dive in!
In this blog, we’ll demonstrate how to set up two different Salesforce orgs for data syncing:
Source Org: The org from which data will be retrieved.
Target Org: The org where data will be accessed from the Source Org.
In this example, we’ll retrieve account record data from the source org and sync it with the target org.
Before syncing data between the orgs, we need to establish a connection between the two orgs. This is where Salesforce Connect comes into play. Want to learn more about Salesforce Connect? Check out the official documentation.
1. Set Up the External Data Source in Target Org
Explain how to connect the target org (the org where you want to display data from the other org) to the source org (the org holding the actual data).
Go to Setup > External Data Sources in the target org.
Click New External Data Source and configure the connection:
Name: Choose a name like "Source Org".
Type: Select Salesforce Connect: Cross-Org Adapter.
URL: Use the URL of the source org.
Authentication > Identity Type: Set up Named Credentials to authenticate the connection securely.
Authentication > Authentication Protocol > Password Authentication
Username: Source Org Username
Password: Source Org Password + Token
Test the connection and, if successful, Sync the data source to bring in the external Account object as an External Object.
After clicking on Sync, it will create an Account object in the external object section with all the account’s fields
2. Creating a Custom Batch Apex to Process Data
To perform operations on external data, use Batch Apex to handle records in batches. This example focuses on retrieving Account records and syncing them to corresponding records in the target org.
Write a Batch Apex class in the target org to read from the external Account data and update the target org’s local Account records.
public with sharing class BatchToSyncExternalAccountRecord implements Database.Batchable<sObject> {
public Database.QueryLocator start(Database.BatchableContext context) {
return Database.getQueryLocator('SELECT Id, Name__c, Industry__c FROM Account__x');
}
public void execute(Database.BatchableContext context, List<Account__x> externalAccounts) {
List<Account> accountsToUpsert = new List<Account>();
for (Account__x externalAccount : externalAccounts) {
// Map fields to target org's Account object
Account accountNew = new Account();
accountNew.External_Id__c = externalAccount.Id;
accountNew.Name = externalAccount.Name__c;
accountsToUpsert.add(accountNew);
}
AccountDomain.upsertAccounts(accountsToUpsert);
}
public void finish(Database.BatchableContext context) {
System.debug(System.label.ACCOUNT_SYNC_COMPLETED);
}
}
3. Create a domain class to perform upsert.
public class AccountDomain {
/**
* This method aims to upsert Account records
* @param List<Account>
* @return N/A
*/
public static void upsertAccounts(List<Account> accountsToUpsert) {
try {
upsert accountsToUpsert Account.External_Id__c;
}
catch (Exception e) {
throw e;
}
}
}
4. Scheduling the Batch Job for Periodic Syncing
public class ScheduleAccountSync implements Schedulable {
public void execute(SchedulableContext context) {
BatchToSyncExternalAccountRecord batchJob = new BatchToSyncExternalAccountRecord();
Database.executeBatch(batchJob, 200);
}
}
To schedule this job, use the Apex Scheduler in the target org. You can do this through Setup > Apex Classes > Schedule Apex.
Define the schedule frequency (e.g., daily or hourly).
Here we are scheduling it every day at 10:10 AM. This is the account Record we created in Source Org :
This record gets automatically created by BatchToSyncExternalAccountRecord Batch at the scheduled time:
Conclusion
By leveraging Salesforce Connect and Batch Apex, you can seamlessly sync data between Salesforce orgs, enabling real-time integration without data duplication. This approach ensures efficient automation, reduces storage overhead, and maintains data consistency across systems. Whether you manage multiple orgs or need live data access, this solution simplifies workflows and enhances operational agility. With scheduled batch jobs, your orgs stay synchronized effortlessly, empowering you to focus on driving business value.
Happy Coding! Please leave a comment to help me understand how the blog helped you. If you need further assistance, don't hesitate to contact us. You can also click "Reach Us" on the website and share the issue with me.
References
https://www.avenoir.ai/post/making-rest-api-callouts-using-the-lwc-component
https://www.avenoir.ai/post/how-to-display-api-response-in-table-using-lwc-component
https://help.salesforce.com/s/articleView?id=platform.platform_connect_about.htm&type=5
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
Blog Credit:
S. Chaudhary
S.Kumar
Salesforce Developer
Avenoir Technologies Pvt. Ltd.
Reach us: team@avenoir.ai
Comments