Dynamics Mobile provides automatic bi-directional synchronization of data between the mobile devices and the ERP.
The synchronization feature is based on the business object definitions created by the mobile developer from Dynamics Mobile Studio. The business objects definitions provides information about the mappings between the mobile storage and the data coming from the ERP.
It allows the mobile device to generate proper xml representation when sending modified data to the ERP.
The synchronization process includes exchange of SyncPackets between the ERP and the mobile device by using Dynamics Mobile as media.
The mobile developer can generate packets from the device by using two techniques:
- Manual SyncPacket Generation
- Automatic SyncPacket Generation
Manual SyncPacket Generation
The mobile developer can use the LiveLink javascript API to send manually SyncPacket to the ERP ( via Dynamics Mobile ). The following snipset illustrates the usage:
var syncPacketString ='...';
LiveLink.postToSyncLog(syncPacketString,function(){alert('success')}
);
The snipset prepares a string variable holding the SyncPacket’s XML and then the packet is sent by using the LiveLink.postToSyncLog method. You should know the exact SyncPacket format and the names of the tables and fields expected by the linked ERP.
However, the framework provides additional features to allow the developer to construct easily SyncPackets from the business objects representation.
var customerBusinessObject = new $ma.bo.SLA.Customer();
customerBusinessObject.no="123";
customerBusinessObject.name="someCustomer";
$ma.liveLink.postToSyncLog(customerBusinessObject,function(){alert('success')});
The code provided creates a business object of type Customer and passes the instance to the LiveLink.postToSyncLog method. It accepts business object instance and automatically generates business object XML representation (based on the Business definition) and will send it to Dynamics Mobile. The developer does not have to care about the actual XML format and the actual mappings. All this will be handled automatically by the framework.
You can also easily convert a collection of objects to SyncPacket
var customerArray = [];
var customerBusinessObject1 = new $ma.bo.SLA.Customer();
var customerBusinessObject2 = new $ma.bo.SLA.Customer();
var customerBusinessObject3 = new $ma.bo.SLA.Customer();
customerArray.push(customerBusinessObject1);
customerArray.push(customerBusinessObject2);
customerArray.push(customerBusinessObject3);
$ma.liveLink.postToSyncLog(customerArray,function(){alert('success')});
The code will generate SyncPacket containing all of the Customer instances. The $ma.liveLink.postToSyncLog() can take array of business object instances, which will be wrapped and sent as a single SyncPacket.
Automatic SyncPacket Generation
The framework provides functionality to automatically generate SyncPackets for the changes performed in the mobile storage. Only the changes performed via the business objects API are tracked. It means that changes done via direct SQL expressions will not be tracked. Consider the following code:
var customerBusinessObject = new $ma.bo.SLA.Customer();
customerBusinessObject.no="123";
customerBusinessObject.name="someCustomer";
customerBusinessObject.Add();
The code creates a new instance of the business object Customer and inserts it into the mobile storage by using the Add() method. The Add() method will insert the instance and will generate and send automatically SyncPacket containing the change.
The developer can also embed multiple business objects into a single SyncPacket:
$ma.liveLink.syncPacket.start();
var customerBusinessObject2 = new $ma.bo.SLA.Customer();
customerBusinessObject.no="123";
customerBusinessObject.name="someCustomer";
customerBusinessObject.Add();
var customerBusinessObject1 = new $ma.bo.SLA.Customer();
customerBusinessObject.no="123";
customerBusinessObject.name="someCustomer";
customerBusinessObject.Add();
$ma.liveLink.syncPacket.post(function(){'success'),function(err){
$ma.liveLink.syncPacket.cancel();
alert(err);});
The code invokes the $ma.liveLink.syncPacket.start() method to instruct the framework, that a new SyncPacket will be generated and sent to the ERP. All subsequent changes to business objects via their Add(), Update() and Delete() methods will be considered as part of the same SyncPacket. The code then creates two instances of the Customer class and inserts them into the mobile storage by calling their Add() methods. Note that the Add() method will not send the business object instance as SyncPacket but will only insert it into the mobile storage and will cache it for sending it later.
The code completes with calling $ma.liveLink.syncPacket.post() method ,which will generate SyncPacket from all business objects changed since the $ma.liveLink.syncPacket.start() method.
NOTES
So, it is recommended to use the automatic SyncPacket generation mode by calling the business objects instances Add(), Update(), Delete() and using the $ma.liveLink.syncPacket.start() and $ma.liveLink.syncPacket.post() to wrap multiple business object changes into single SyncPacket.
The single SyncPacket wrapping technique is vital when the back-end system (ERP) requires all the data for a particular change in a single SyncPacket. For example the changes for invoice document, which contains header and lines are a good candidates to be wrapped into a single SyncPacket.
You must also have in mind that there is difference in the actual behavior of the application when running inside a web browser and inside a native mobile app.
The web browser app will try to send the packet immediately to Dynamics Mobile by performing HTTP REST request.
The mobile app will cache the packet on the device and it will be sent later automatically behind the scene. Usually it takes up to 5 minutes for the packet to be actually sent to Dynamics Mobile if the device is connected permanently to Internet.
If the user performs manual synchronization, all pending (cached and not sent) packets will be sent to Dynamics Mobile immediately.