How to Build an Offline Salesforce App, Part 4: Synchronize Client with Server
In this multi-part tutorial, you will discover how to integrate your Salesforce data seamlessly into an OutSystems mobile application. You will learn to incorporate the Salesforce connector, authenticate with Salesforce, pull data from Salesforce’s servers, and cache the data for use in your mobile application.
In part three, Retrieve the Access Token, you took the steps necessary to cache the user’s Salesforce access token. With the access token, your user is capable of retrieving data from the Salesforce REST API. And in part four, you will create synchronization actions to pull and push customer data from client to server and vice versa by following these steps:
- Make a temporary copy of the Salesforce Lead entity.
- Create a local version of the entity.
- Setup default synchronization actions.
- Replace LeadEntity with Salesforce requests.
- Delete LeadEntity.
Watch the video for the full, step-by-step process or read along for an abridged description of the video’s content.
1. Make a Temporary Copy of the Salesforce Lead Entity
The Salesforce connector provides a Lead entity that represents the customers in your Salesforce database. Make a copy of this entity in your database and name it LeadEntity, then remove any of the attributes you don’t need. In our example, we retained Id, FirstName, MiddleName, LastName, Title, and Company—we deleted the remaining attributes.
Why are we duplicating this entity if it already exists in the Salesforce connector? Because it enables a neat shortcut in step three. The shortcut only applies to entities that belong exclusively to your OutSystems database, so you must create a copy before exploiting this neat synchronization trick.
2. Create a Local Version of the Entity
For the next step in this trick, you need to make a copy of LeadEntity into Local Storage, thereby creating LocalLeadEntity. To do so, highlight LeadEntity in your database, make a copy, then highlight Local Storage, and paste. This step seems innocent enough, but it actually binds LeadEntity to LocalLeadEntity in your application’s architecture. We require this relationship before we can perform step three’s nifty shortcut.
3. Set Up Default Synchronization Actions
You’ve made it to step three and you probably can’t wait a moment longer to discover what this super-awesome-cool trick is. In this step, you will establish a handful of default synchronization actions on your mobile client. Synchronization is critical to your offline application’s success because it enables users to manipulate their local data without network access, and then push their modifications to the server once the device regains access to a network.
To complete this step, right-click your LocalLeadEntity and select Create Action to Sync Data (Read/Write). This quick step will setup flows that create, update, and delete LeadEntity objects that the user has modified. The flows will also fetch the latest LeadEntity objects from the server to populate the client’s Local Storage. But we don’t want to synchronize LeadEntity objects, we want to synchronize with Salesforce instead—and we’ll handle that in the next step.
4. Replace LeadEntity with Salesforce requests
LeadEntity was a red herring—after all, that table was empty and was of no value to you. So in this step, you will replace server-side LeadEntity actions with Salesforce API requests. To begin, find all usages for LeadEntity or go directly to the SyncLeadEntities action (created automatically for you in the previous step). This action creates, updates, deletes, and fetches LeadEntity objects — so you must replace these actions one by one.
Find and add the following from the Salesforce connector to your SyncLeadEntities action: LeadCreate, LeadUpdate, LeadDelete, and LeadFind. Begin to replace the LeadEntity actions with those you added from the Salesforce connector. This will require you to assign the sources of the LeadEntity actions as the sources for your new Salesforce actions, but attribute mapping should happen automatically.
Note: if you haven’t already, change LocalLeadEntity’s Id data type from LeadEntity.Identifier to text. Salesforce Lead entities have text-based Ids, not Ids provided for OutSystems objects.
5. Delete LeadEntity
With your synchronization action prepared to use Salesforce data, the shortcut is complete and you can safely delete LeadEntity from your database.
In the next part of this tutorial, you will activate synchronization at key points of your application, aggregate LocalLeadEntity objects, and prepare them for display on the home screen.