capture contact to sync to crm

Ivan Louw asked on August 15, 2019 03:58

Hi everyone, I am using Kentico 12 MVC. I have a form where customers can complete details. These details are mapped to contacts, but I need to get the contact into our crm system.

I have added the following code on the fromhandler module in the website.

protected override void OnInit()
{
    base.OnInit();

    // Assigns a handler to the BizFormItemEvents.Insert.After event
    // This event occurs after the creation of every new form record
    BizFormItemEvents.Insert.After += FormItem_InsertAfterHandler;

    // Assigns a handler to the IntegrationEvents.LogInternalTask.After event
    // This event occurs after the system creates outgoing integration tasks (separately for each task)
    //IntegrationEvents.LogInternalTask.After += LogIntegrationTask_After;
    //IntegrationEvents.LogInternalTask.Before += LogIntegrationTask_Before;

ContactInfo.TYPEINFO.Events.LogChange.Before += LogContactInfoObjectChange_Before;

}

    private void LogContactInfoObjectChange_Before(object sender, LogObjectChangeEventArgs e)
{
    // Gets the synchronized object
    GeneralizedInfo obj = e.Settings.InfoObj;
    e.Settings.LogStaging = false;
    // Gets the info object for an integration connector
    IntegrationConnectorInfo connectorInfo = IntegrationConnectorInfoProvider.GetIntegrationConnectorInfo("CRMConnector");

    if ((connectorInfo != null) && (e.Settings.InfoObj.MainObject != null))
    {
        // Gets an instance of the integration connector class
        BaseIntegrationConnector connector = IntegrationHelper.GetConnector(connectorInfo.ConnectorName) as BaseIntegrationConnector;

        IntegrationTaskEventArgs task = new IntegrationTaskEventArgs();
        task.Object = e.Settings.InfoObj.MainObject;
        task.Task = new IntegrationTaskInfo();
        //task.Task.TypeInfo = new ObjectTypeInfo(
        // Synchronizes the processed integration task
        connector.ProcessInternalTask(task.Task);
    }

}


Currently I am getting the following error.
System.Exception: '[IntegrationHelper.LogSynchrnoizationError]: Synchronization is not set.'

System.Exception HResult=0x80131500 Message=[IntegrationHelper.LogSynchrnoizationError]: Synchronization is not set. Source=CMS.Synchronization StackTrace: at CMS.Synchronization.IntegrationHelper.LogSynchronizationError(IntegrationSynchronizationInfo synchronization, String result) at CMS.SynchronizationEngine.BaseIntegrationConnector.ProcessInternalTasks(IEnumerable1 tasks) at Website.Modules.FormHandler.FormHandlerModule.LogContactInfoObjectChange_Before(Object sender, LogObjectChangeEventArgs e) in D:\Solutions\website\Modules\FormHandler\FormHandlerModule.cs:line 188 at CMS.Base.AbstractHandler.CallEventHandler[TArgs](EventHandler1 h, TArgs e) at CMS.Base.AbstractHandler.Raise[TArgs](String partName, List1 list, TArgs e, Boolean important) at CMS.Base.AdvancedHandler2.RaiseBefore(TArgs e) at CMS.Base.AdvancedHandler2.RaiseBefore(TArgs e) at CMS.Base.AdvancedHandler2.StartEvent(TArgs e, Boolean allowEvent)

Any help will be appreciated. Once the message is on the integration bus, I can capture the message and process it.

Recent Answers


David te Kloese answered on August 15, 2019 10:12

Hi,

First I assume you've enabled the integration bus settings?

Image Text

And created the actual connector "CRMConnector" including code?

Image Text

By default you subscribe to certain events and let your connector handle it. e.g.: docs.kentico.com/.../implementing-outgoing-synchronization.

Is there a reason you don't want to use it this way? If it's because you want it to be synchronized automatically you could also setup an event to do that, an example : docs.kentico.com/.../automatically-synchronizing-staging-and-integration-tasks


Looking at your code you are actually triggering the handling of the task by connector.ProcessInternalTask(task.Task). But think not all properties of IntegrationTaskEventArgs task are set...

I'd look into setting more of those properties, but that requires some logic which Kentico could handle out of the box for you. So I'd say look at the sample in my first link.

0 votesVote for this answer Mark as a Correct answer

Ivan Louw answered on August 15, 2019 14:27

Hi David,

Thanks for your help.

Your assumptions are correct. All the settings you mentioned have been set.

The reason behind this change from the documented approach is because the event are only triggered in the admin site when creating a contact but not when completing the form and mapping the fields to a contact. Then no event is raised.

Here is the code of the connector, I have not included the handling as the code segment will be to large.

  public class CRMConnector : BaseIntegrationConnector
    {
        public override void Init()
        {
            // Initializes the connector name
            ConnectorName = CRMConnectorHelper.ConnectorName;  //GetType().Name;
            // Register subscriptions - Contact Object type
            CMSEventLogging.CMSEventLog(EventType.INFORMATION, "CRMConnector", "INITIATECONNECTOR", "CRM Connector Starting");
            SubscribeToObjects(TaskProcessTypeEnum.AsyncSnapshot, ContactInfo.OBJECT_TYPE, TaskTypeEnum.CreateObject);
            SubscribeToObjects(TaskProcessTypeEnum.AsyncSnapshot, ContactInfo.OBJECT_TYPE, TaskTypeEnum.UpdateObject);

            ObjectIntegrationSubscription customTableEmailSub = new ObjectIntegrationSubscription(ConnectorName, TaskProcessTypeEnum.AsyncSimpleSnapshot, TaskTypeEnum.CreateObject, null, CustomTableItemProvider.GetObjectType("customtable.Email"), null);
            SubscribeTo(customTableEmailSub);

            //SMS custom table
            ObjectIntegrationSubscription customTableSmsCreateSub = new ObjectIntegrationSubscription(ConnectorName, TaskProcessTypeEnum.AsyncSimpleSnapshot, TaskTypeEnum.CreateObject, null, CustomTableItemProvider.GetObjectType("customtable.SMS"), null);
            SubscribeTo(customTableSmsCreateSub);
            ObjectIntegrationSubscription customTableSmsUpdateSub = new ObjectIntegrationSubscription(ConnectorName, TaskProcessTypeEnum.AsyncSimpleSnapshot, TaskTypeEnum.UpdateObject, null, CustomTableItemProvider.GetObjectType("customtable.sms"), null);
            SubscribeTo(customTableSmsUpdateSub);

            //ObjectIntegrationSubscription customTableDataSub = new ObjectIntegrationSubscription(ConnectorName, TaskProcessTypeEnum.AsyncSimpleSnapshot, TaskTypeEnum.All, null, CustomTableItemProvider.GetObjectType("customtable.SMS"), null);
            //SubscribeTo(customTableDataSub);

            ////subscribe to seminar registration
            //SubscribeToObjects(TaskProcessTypeEnum.AsyncSnapshot, "bizformitem.BizForm.Seminar_asPerLiveSite");


        } 

        public override IntegrationProcessResultEnum ProcessInternalTaskAsync(GeneralizedInfo infoObj, TranslationHelper translations, TaskTypeEnum taskType, TaskDataTypeEnum dataType, string siteName, out string errorMessage)
        {
            CMSEventLogging.CMSEventLog(EventType.INFORMATION, "SyncCMS", "Triggered", "Start");
            errorMessage = null;
0 votesVote for this answer Mark as a Correct answer

Ivan Louw answered on August 15, 2019 14:30

I has been suggested to register the connector in both website projects. But this feels to me to be not the correct approach as I would rather use the in build functionality. It just that the in build functionality seems not to fire on the website, only on the admin site.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.