The below tutorial will cover what Project Automation is and how it can be used.
The Project Automation feature allows you to run your own .NET C# code at certain points in the Data Sync process.
Project automation can be opened by going to View
> Project Automation Window
. To then begin using this you need to click Enable Project Automation
.
This will then open the code view.
The Start()
method allows you to adjust configuration settings on your source and target data connections before the project runs.
This can be used to change a filter dynamically at runtime.
The End()
Method allows you to run something when the process is complete.
This can be used to call a SQL Stored Procedure or Log the result somewhere else.
The standard events contained within Project Automation are as follows :-
Item Events allow you to intercept before and after an operation during the sync process.
You can cancel an operation by setting the Sync property to false
in the Before()
event this is the same as unchecking the Sync flag in the comapre results window except this allows you to do it programatically.
The item field contains details about the item to be either Added, Updated or Deleted.
The identity field may contain the ID value of the target item this depends on the provider implementation.
One use-case is where you want to update the source system with the created ID value from the target, for example where in Dynamics CRM you want to add the Entity Guid to your source SQL Table whenever a new account is added. This can be done easily via the UpdateSourceRow helper function on the SQL Data Connector.
public override void AfterAddItem(object sender, DataCompareItemInvariant item, object identity)
{
DataSourceA.UpdateSourceRow("CRM_ID", identity, item);
}
Another Example is using this to clear a Sync flag on the source SQL Table after it has been written to the target.
public override void AfterAddItem(object sender, DataCompareItemInvariant item, object identity)
{
DataSourceA.ExecuteNonQuery(string.Format("UPDATE {0} SET [Sync]=0 WHERE [ID]=?", DataSourceA.SourceTable), item.Key);
}