Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
using FuseIT.S4S.WebToSalesforce.Connection;
using FuseIT.Sitecore.Personalization.Facets;
using FuseIT.Sitecore.Salesforce;
using FuseIT.Sitecore.SalesforceConnector;
using FuseIT.Sitecore.SalesforceConnector.Entities;
using FuseIT.Sitecore.SalesforceConnector.Services;
using Sitecore.XConnect;
using Sitecore.XConnect.Client;
using Sitecore.XConnect.Client.Configuration;
using Sitecore.XConnect.Collection.Model;
using System;
using System.Collections.Generic;
using XConnect = Sitecore.XConnect;
using BaseSitecore = Sitecore;

namespace FuseIT.S4SSyncc.Demo
{
    public class SyncSalesforceToSitecore
    {
        public const string SFFieldName_FirstName = "FirstName";
        public const string SFFieldName_LastName = "FirstName";
        public const string SFFieldName_Title = "Title";
        public const string SFFieldName_Birthdate = "Birthdate";
        public const string SFFieldName_SitecoreAliasId = "FuseITAnalytics__SitecoreVisitorId__c";
        public const string SFFieldName_Description = "Description";
        public const string SFFieldName_Mobile = "MobilePhone";
        public const string SFFieldName_LeadSource = "LeadSource";

        public void SyncSaleforceFieldsIntoSitecoreContactFacetsNew()
        {
            //Define the SOQL query to retrieve Salesforce objects. Make sure to include all the Salesforce field names which need get the values from in order to set them into Sitecore facets.
            //If you do not define the Salesforce field names in this SOQL query which you intent to read later, code will not bring back those Salesforce fields or values.
            var soqlQuery = @"Select c.Id, c.FirstName, c.LastName, c.Birthdate, c.Title, c.Description, c.MobilePhone, c.LeadSource, c.FuseITAnalytics__SitecoreVisitorId__c  from Contact c where LastModifiedDate  >= LAST_N_DAYS:1";

            //Define Salesforce entity type. This type should match with the SOQL query above.
            var sfObjectType = "Contact";

            //Define S4S connection string name. Default one would be "S4SConnString".
            var s4sConnectionStringName = "S4SConnString";

            //Get Salesforce session
            var salesforceSession = SalesforceSessionCache.GetSalesforceSession(s4sConnectionStringName);

            //Get relevant Salesforce entities according to the SOQL query.
            var sfEntityList = GetSFEntitiesBySOQLQuery(salesforceSession, sfObjectType, soqlQuery);

            foreach (var sfEntity in sfEntityList)
            {
                var sitecoreAliasId = (sfEntity.InternalFields.Contains(SFFieldName_SitecoreAliasId)) ? sfEntity.InternalFields[SFFieldName_SitecoreAliasId] : string.Empty;
                if (!string.IsNullOrEmpty(sitecoreAliasId))
                {
                    //Read relevant Salesforce field values            
                    var firstName = (sfEntity.InternalFields.Contains(SFFieldName_FirstName)) ? sfEntity.InternalFields[SFFieldName_FirstName] : string.Empty;
                    var lastName = (sfEntity.InternalFields.Contains(SFFieldName_LastName)) ? sfEntity.InternalFields[SFFieldName_LastName] : string.Empty;
                    var title = (sfEntity.InternalFields.Contains(SFFieldName_Title)) ? sfEntity.InternalFields[SFFieldName_Title] : string.Empty;
                    DateTime birthdate = (sfEntity.InternalFields.Contains(SFFieldName_Birthdate)) ? sfEntity.InternalFields.GetField<DateTime>(SFFieldName_Birthdate) : DateTime.MinValue;
                    var description = (sfEntity.InternalFields.Contains(SFFieldName_Description)) ? sfEntity.InternalFields[SFFieldName_Description] : string.Empty;
                    var mobilePhone = (sfEntity.InternalFields.Contains(SFFieldName_Mobile)) ? sfEntity.InternalFields[SFFieldName_Mobile] : string.Empty;
                    var leadSource = (sfEntity.InternalFields.Contains(SFFieldName_LeadSource)) ? sfEntity.InternalFields[SFFieldName_LeadSource] : string.Empty;

                    using (XConnectClient client = SitecoreXConnectClientConfiguration.GetClient())
                    {
                        try
                        {
                            //Include all the Sitecore facet keys which you would like to read/write.
                            //For this example, following code will retrieve "Personal" and "S4SInfo" facets.
                            var facetNameArray = new string[] { PersonalInformation.DefaultFacetKey, S4SInfo.DefaultFacetKey };

                            //Retrieve Sitecore contact by alias id
                            IdentifiedContactReference reference = new IdentifiedContactReference(XConnect.Constants.AliasIdentifierSource, sitecoreAliasId);
                            var sitecoreContact = client.Get<XConnect.Contact>(reference, new ContactExecutionOptions(new ContactExpandOptions(facetNameArray)));

                            if (sitecoreContact != null)
                            {
                                Logging.Debug(this, $"{nameof(SyncSaleforceFieldsIntoSitecoreContactFacetsNew)}(): Existing Sitecore contact found for source: {XConnect.Constants.AliasIdentifierSource} and Identifier: {sitecoreAliasId}.");
                               
                                #region SETTING PERSONAL FACET - FirstName, LastName, Title, Birthdate
                                //Set FirstName, LastName, Title and Birthday fields in PersonalInformation facet

                                var personalFacet = sitecoreContact.Personal() ?? new PersonalInformation();

                                if (!string.IsNullOrEmpty(firstName))
                                {
                                    personalFacet.FirstName = firstName;
                                    Logging.Debug(this, $"{nameof(SyncSaleforceFieldsIntoSitecoreContactFacetsNew)}(): Values set to the personal information facet FirstName - [{firstName}].");
                                }

                                if (!string.IsNullOrEmpty(lastName))
                                {
                                    personalFacet.LastName = lastName;
                                    Logging.Debug(this, $"{nameof(SyncSaleforceFieldsIntoSitecoreContactFacetsNew)}(): Values set to the personal information facet LastName - [{lastName}].");
                                }

                                if (!string.IsNullOrEmpty(title))
                                {
                                    personalFacet.Title = title;
                                    Logging.Debug(this, $"{nameof(SyncSaleforceFieldsIntoSitecoreContactFacetsNew)}(): Values set to the personal information facet Title - [{title}].");
                                }

                                if (birthdate != DateTime.MinValue)
                                {
                                    personalFacet.Birthdate = BaseSitecore.DateUtil.ToUniversalTime(birthdate);
                                    Logging.Debug(this, $"{nameof(SyncSaleforceFieldsIntoSitecoreContactFacetsNew)}(): Values set to the personal information facet Birthdate - [{birthdate.ToString()}].");
                                }

                                client.SetPersonal(sitecoreContact, personalFacet);

                                #endregion

                                #region SETTING S4SINFO FACET - LeadSource, MobilePhone, Description
                                //SET LeadSource, MobilePhone and Description values in dictionary facet field called Fields in S4SInfo facet.

                                var s4SFacets = sitecoreContact.GetFacet<S4SInfo>(S4SInfo.DefaultFacetKey) ?? new S4SInfo();

                                if (!string.IsNullOrEmpty(description))
                                {
                                    if (s4SFacets.Fields.ContainsKey("Description"))
                                    {
                                        s4SFacets.Fields["Description"] = description;
                                    }
                                    else
                                    {
                                        s4SFacets.Fields.Add("Description", description);
                                    }

                                    Logging.Debug(this, $"{nameof(SyncSaleforceFieldsIntoSitecoreContactFacetsNew)}(): Add/edit following key value pair to the Field dictionary in S4SInfo facet: Key-[Description], Value-[{description}].");
                                }

                                if (!string.IsNullOrEmpty(mobilePhone))
                                {
                                    if (s4SFacets.Fields.ContainsKey("MobilePhone"))
                                    {
                                        s4SFacets.Fields["MobilePhone"] = mobilePhone;
                                    }
                                    else
                                    {
                                        s4SFacets.Fields.Add("MobilePhone", mobilePhone);
                                    }

                                    Logging.Debug(this, $"{nameof(SyncSaleforceFieldsIntoSitecoreContactFacetsNew)}(): Add/edit following key value pair to the Field dictionary in S4SInfo facet: Key-[MobilePhone], Value-[{mobilePhone}].");
                                }

                                if (!string.IsNullOrEmpty(leadSource))
                                {
                                    if (s4SFacets.Fields.ContainsKey("LeadSource"))
                                    {
                                        s4SFacets.Fields["LeadSource"] = leadSource;
                                    }
                                    else
                                    {
                                        s4SFacets.Fields.Add("LeadSource", leadSource);
                                    }

                                    Logging.Debug(this, $"{nameof(SyncSaleforceFieldsIntoSitecoreContactFacetsNew)}(): Add/edit following key value pair to the Field dictionary in S4SInfo facet: Key-[LeadSource], Value-[{leadSource}].");
                                }

                                //Set S4S dictionary facet
                                client.SetFacet(sitecoreContact, S4SInfo.DefaultFacetKey, s4SFacets);

                                #endregion

                                // Submits the batch.
                                client.Submit();
                            }
                            else
                            {
                                Logging.Debug(this, $"{nameof(SyncSaleforceFieldsIntoSitecoreContactFacetsNew)}(): No existing Sitecore contact found for source: {XConnect.Constants.AliasIdentifierSource} and Identifier: {sitecoreAliasId}.");
                                continue;
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.Error(this, $"{nameof(SyncSaleforceFieldsIntoSitecoreContactFacetsNew)}(): Exception occured when retrieving/updating Sitecore contact with Sitecore alias id: {sitecoreAliasId}", ex);
                            continue;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Retrieve Salesforce entities by SOQL query
        /// </summary>
        /// <param name="sfSesion"></param>
        /// <param name="sfEntityType"></param>
        /// <param name="soqlQuery"></param>
        /// <returns></returns>
        private List<GenericSalesforceEntity> GetSFEntitiesBySOQLQuery(SalesforceSession sfSesion, string sfEntityType, string soqlQuery)
        {
            var sfEntityList = new List<GenericSalesforceEntity>();
            try
            {
                Logging.Debug(this, $"{nameof(GetSFEntitiesBySOQLQuery)}(): EntityType: {sfEntityType} SOQLQuery: {soqlQuery}, ");
                var genericSFService = new GenericSalesforceService(sfSesion, sfEntityType);
                sfEntityList = genericSFService.GetBySoql(soqlQuery);
            }
            catch (Exception ex)
            {
                Logging.Error(this, $"{nameof(GetSFEntitiesBySOQLQuery)}(): An error occured when retrieving Salesforce entities using SOQL query.", ex);
            }

            return sfEntityList;
        }
    }
}

To run this regularly sync the two systems you can , hook this method into a Sitecore scheduled task.

...