Sitecore 9 Dynamic CRM Connector: Conditionally Sync Sitecore Field (Part 6)

Sitecore 9 Dynamic CRM Connector: Conditionally Sync Sitecore Field (Part 6)

Part 5: Schedule Pipeline Batches in Sitecore 9 Dynamic CRM Connector 

There can be the situation while you are syncing xDB contacts to CRM and you want to update some field on the basis of some logic. Just like the below condition where I wanted to sync to “Birthdate” when it is above 1990 otherwise, it will be blank in CRM.

Apply Mapping Rules

  • To apply mapping rules firstly create custom class “BirthdateValueMappingRule”
       
 namespace Namespace  
 {  
   public class BirthdateValueMappingRule : IApplyMappingRule  
   {  
     public readonly int Year = 1990;  
     public readonly string FacetName = "Personal";  
     public bool ShouldMappingBeApplied(object value, IMapping mapping, MappingContext context)  
     {  
       if (value != null && context != null)  
       {  
         var MappingSource = (EntityModel)context.Source;  
         var MappingFacets = MappingSource?.Facets;  
         if (MappingFacets != null)  
         {  
           foreach (var facet in MappingFacets)  
           {  
             if (facet.Key.Equals(FacetName))  
             {  
               var val = (PersonalInformation)facet.Value;  
               if (val.Birthdate != null)  
               {  
                 DateTime date = (DateTime)val.Birthdate;  
                 if (date.Year > Year)  
                 {  
                   return true;  
                 }  
                 else  
                 {  
                   return false;  
                 }  
               }  
               else  
               {  
                 return true;  
               }  
             }  
           }  
         }  
       }  
       return true;  
     }  
   }  
 }  
  • Create another class “BirthdateValueMappingRuleConvertor”

 namespace Namespace  
 {  
   public class BirthdateValueMappingRuleConvertor : BaseItemModelConverter<IApplyMappingRule>  
   {  
     private static BirthdateValueMappingRule _rule;  
     public BirthdateValueMappingRuleConvertor(IItemModelRepository repository)  
      : base(repository)  
     {  
     }  
     protected override ConvertResult<IApplyMappingRule> ConvertSupportedItem(  
      ItemModel source)  
     {  
       if (_rule == null)  
         _rule = new BirthdateValueMappingRule();  
         return PositiveResult(_rule);  
     }  
   }  
 }  
  • In Sitecore, navigate to this location “/sitecore/system/Data Exchange/XXX/Data Access/Apply Mapping Rules/Providers” and create “xConnect Apply Mapping Rules” item
xConnect Apply Mapping Rules
  • Create an item
    • Template: /sitecore/templates/Data Exchange/Framework/Data Access/Apply Mapping Rules/Null Value Mapping Rule
    • Item Name: “Birthdate Mapping Rule”
Null Value Mapping Rule

  • Update “Converter Type” field with “Namespace.BirthdateValueMappingRuleConvertor,AssemblyName”
Contact Model to Dynamics Contact
  • Navigate to this location “/sitecore/system/Data Exchange/XXX/Value Mapping Sets/xConnect Contact to Dynamics Contact Mappings/Contact Model to Dynamics Contact/Birthdate” 
  • In “Apply Mapping Rules” field select “Birthdate Mapping Rule”
Contact Model to Dynamics Contact

  • Now sync “xConnect Contacts to Dynamics Sync”

Comments

Popular Posts