Update A Source SQL Table after a Import to Dynamics CRM

After importing data to Dynamics CRM you may require the source to be updated with some information such as the import or Update date

After importing data to Dynamics CRM you may require the source to be updated with some information such as the import or Update date.

DS3 is designed to allow you to extend its processes at any point. In this case we can use the 'Project Automation' feature to look back at the source SQL Table and update a 'Imported' column to the date of the import.

To activate the 'Project Automation' feature select the View -> Project Automation Window menu item.

Project Automation

A new tab will appear in the middle section of DS3 titled 'Project Automation'. Click the 'Enable Project Automation' Link.<

Project Automation Window

To execute some code when the synchronisation finished we can write some code in the End method.

End Method

Firstly, we must reference the SQL Utility class called Massive in the usings section:

Usings

using Simego.DataSync.Helpers.Massive;

Then we use the following code in the end method:

public override void End(ProjectAutomationResult result)
    {
        ///Set the Date and Time we want to appear in the imported column in the SQL Source Table
        var DateNow = DateTime.Now;
        
        //Create a SQL utility table using the Source Details 
        var model = new< DynamicModel(DataSourceA.ConnectionString, DataSourceA.SourceTable, "ID", "System.Data.OleDb");
                
        //Only run if there has been chaned and it has been successfull
        if ( result.HasChanges &amp;&amp; result.Success ) 
        {
           //Run through the items that have been updated
            foreach(var update in result.CompareResult.Updated) 
            {
                //Retrieve the value of the Source key (in this case it is in the first column)
                int id = int update.SourceRow[0].BeforeColumnValue;
                
               //Update the source table
                   model.Update(
                    new { 
                        Imported = DateNow
                    },id );     
            }
       
            //Run through the items that have been created
           foreach(var update in result.CompareResult.New) 
            {
                //Retrieve the value of the Source key (in this case it is in the first column)
                int id = int update.SourceRow[0].BeforeColumnValue;
                //Update the source table
                   model.Update(
                    new { 
                        Imported = DateNow
                    },id );     
            }
        }
    }


Can't find the answers you're looking for? Contact our support team and we'll personally get back to you as quickly as we can.