LCFM Native provides the ability to sync all your data from your FileMaker Server between your apps on desktop and mobile. But what if you only want to sync a small portion of data at a time? This lesson covers how you can sync individual tables, for example if you want to show a logged in customer only their own data and nobody else's.
Note: you will need to be using LCFM Native 1.5 Beta 4 or later for this lesson to work.
You can download the sample FileMaker Solution here.
If you haven't done this already, open up LCFM Native Beta 4 or later. The first time you open it, you will see a dialog that says "installing Extended Script Steps Plugin". This is when LCFM installs the script steps you will need into your copy of FileMaker. Once it has done this, you can close LCFM again.
For the purpose of this demo, we need a file that has multiple customer accounts, and a login screen. We're going to build the simplest possible file here as this is just a demo, but you should be able to use the principles in a more complicated app. Open up FileMaker, and save a new blank file as Customers.fmp12.
Add a Name field to the Customers table. Then create a new table named Customer Data, and add the fields PurchaseDate, Item and Customer_ForeignKey. PurchaseDate should be a date, the others are text.
We now need to set up a relationship between PrimaryKey in Customers and Customer_ForeignKey in Customer Data. Go to Relationships. You may need to expand the relationship field a little to see Customer_ForeignKey at the bottom of the Customer Data window (1). Click and drag on PrimaryKey in Customers to connect to Customer_ForeignKey in Customer Data (2).
Now click on the "=" between the linked fields (3) and you will see this:
Check the box "Allow creation of records in this table via this relationship", on the Customer Data side. We have now linked the customer ID to the customer data for a specific customer. Click ok to everything and you should be on the Customer layout, with a Name field visible. Click on Edit Layout at the top right.
Add a portal (1). Choose Show Records from Customer Data (2). Click Ok (3).
Add PurchaseDate and Item to Included fields. Click OK. Click Exit Layout and save.
We now need some customer records and some data. Go to Customers, create a new record (1), and enter a name (2). In the Portal, enter some dates and items (3). Do this two or three more times.
We now need a way to make the data visible to the whole app so that we can select the bits we want. To do this we need another table. Go to File>Manage>Database and add a table called Globals. On this we need two fields called gCustomerName and gCustomerKey, both text fields. We don't need the 5 default fields on this table, so we can delete those. You should end up with this:
Both these fields need global storage set. Click on Options (1), Storage (2) and check the box "use global storage" (3).
In Relationships, connect gCustomerName and name:
Now go back to the Fields pane (1), and change gCustomerKey (2) to "Calculation" (3). In the script for this enter:
Lookup ( Customers::PrimaryKey )
We have set gCustomerKey to the value of the appropriate PrimaryKey in Customers, when gCustomerName is set to the name of a customer. This allows us to link together our dataset with a specific customer.
We now need a way for a customer to log in, so we can define their data by account. Create a new layout from View>Layout Mode>New Layout and call it Login. Show Records from Globals:
Now we want a dropdown field (1) connected to the gCustomerName field:
Click Ok, then in the Inspector, click on the pencil (circled) next to Values from:
Click New (circled below) to create a new value list:
Then click "Use values from field" (1), choose Customers (2) and select "name" (3).
Next we need another layout, called Logged in. Again this should show records from Globals. On this layout we want a portal with Customer Data fields Item and PurchaseDate:
Add a text field with the content:
Add a button, with the text "Change Login". To this add a single step,
In the Steps, click Navigation (1), then Go to Layout (2), and select Login (3). Click ok. Exit layout and save.
Finally, we need to make sure that when we open our built app, it opens on the login screen. Go to File> File options... and check "Switch to layout" (1). Click Specify (2), and choose "Login" (3).
We're now ready to add the brand new syncing script step that will allow us to only see and sync data for the logged in customer.
Go to the Login layout, and add a button. Choose "Perform Script".
In the dialog that appears click the + button, circled (1). Name the new script "Login" (2). Click OK (3).
Scroll down the list of Steps on the right, and expand the LCFM Native plugin steps. Double click on "Set Table Sync Constraint" (1). Click in the first square box in the script (2).
In the dialog that appears when you click in the box (1), select Customer Data (2) and then either select or start typing Customer_ForeignKey. Double click to select it, click ok.
Now click in the second box, and select the table Globals and the field gCustomerKey.
Next in the list of LCFM script steps, double click "Set Automatic Sync Enabled" (1). Click in the blank box and enter "true" (2) in the dialog that appears. Click ok.
Click enter to go to the next line, then start typing "Go to layout". An auto select box will come up, double click on Go to Layout.
Click on the "original layout" default text, and select the "Logged in" layout. Double click to enter.
That's the full script we need created, so click "OK", and then OK again. Save everything.
Obviously, this is a very basic example. You could do things like add a log out button to the logged in layout, and add some validation to the login button to make sure the customer exists, plus making it all a lot prettier, but for our purposes, this will do. We should now be able to demonstrate that data is synced for the logged in customer only.
Open up LCFM Native, import your Customer.fmp12 file, (don't forget to set a username and password for it) and choose Regular Start. Fast Start would skip the syncing settings, which we will need. Build your file in the usual way, include all layouts and all tables, and choose yes on the Data options screen, to both synchronise data and extract data.
On the Sync confirmation screen, you will see that customers and customer data have all the necessary fields for syncing, because we left in the default fields at the start. Globals does not because we deleted them, but that's fine because we don't need Globals to sync. So click Yes, Continue.
You will arrive on the Syncing Options screen. For the most efficient experience, I have set the syncing check time to 5 seconds, the time between checks for deleted records to the maximum possible, and retry to connect to server to 3 times.
Set the fmrest permissions on your new interface file and on your original Customers.fmp12 file. Set the password on your interface file to the same as your Customers file. Upload both to the server. Enter your server address in LCFM Native and click next.
I'm choosing to build for Mac for the purposes of this demo. You can of course build for iOS or Android.
On the next screen, before I test my solution, I need to turn off automatic syncing. In the top right corner (1) click on "Edit Deployment Profile". Choose the "Sync Settings" tab (2), and uncheck the "Automatic sync enabled at startup" box (3). Click "done" (4).
Now click the test button to build your Mac app and open it on your desktop.
We are now ready to demonstrate that I am able to log in as a specific customer and see, add and sync only the data for that customer.
In my desktop app, I choose to log in as Ali from the drop down menu. I can see he has two items, Bee and Fly in his account:
Now I open the Customers solution on the server, and log in to Ali's account. I click New Record, and add Mosquito:
Head back over to the desktop app and it has updated Ali's record:
To confirm we've not updated anyone elses records, I change login, and check Chris.
And thats it. We are syncing a single customer account. Obviously there are many refinements you could apply to this extremely basic app, but this should demonstrate the use of the Set Table Sync Constraint script step.