Let’s face it.. sometimes we don’t care.. But how do we explain this to Dynamics?

By | April 24, 2018

Imagine that some data is being pushed to Dynamics through SSIS or other tool, and that data includes associations between non-existing users and their roles/teams. Your data migration script will diligently fail when trying to push those associations to Dynamics, just as it should. But what if, this time, you just wanted to ignore those errors without having to update the data migration script?

I admit this is a weird scenario, and I also admit that it goes against the rules. But, when you have little time to do something and not a lot of options, this is the kind of thing you may come up with.

Turns out that, at least in 8.2, we can register a plugin on the Associate message, we can use that plugin to remove entity references for the entities being associated from the plugin context, and Dynamics will just play along by completely ignoring that associate request.

In my case, I registered that plugin in the Pre-Validation stage assuming that it may be too late to do it in Pre-Operation. That assumption might or might not be correct, but those are finer details.. They did not matter as long as it worked.

And below is the code, just in case you ever need to do something similar. This code will check if it’s running on the “Associate” message, and, if that’s the case, it will check if either the target or the related entity is, actually, a “systemuser” entity (you will also see systemuserid there.. surprisingly, that’s how logical name shows up when a user is being added to a team). Once that’s confirmed, it will check if the user exists. And, if there is no such users, it will remove Target from the context, and it will cleanup related entities.

 

public  bool UserExists(IOrganizationService service, EntityReference userRef)
        {
            try
            {
                var user = service.Retrieve("systemuser", userRef.Id, new ColumnSet());
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
           
           if (context.MessageName == "Associate")
           {
                string relationshipName = null;
                if (context.InputParameters.Contains("Relationship"))
                {
                    relationshipName = ((Relationship)context.InputParameters["Relationship"]).SchemaName;
                }
                var targetEntity = (EntityReference)context.InputParameters["Target"];
                var relatedEntities = context.InputParameters["RelatedEntities"] as EntityReferenceCollection;
                var relatedEntity = relatedEntities[0];

               if ((targetEntity.LogicalName.ToLower() == "systemuser" || targetEntity.LogicalName.ToLower() == "systemuserid") && !UserExists(service, targetEntity))
               {
                    context.InputParameters.Remove("Target");
                    relatedEntities.Clear();
                }
                if ((relatedEntity.LogicalName.ToLower() == "systemuser" || relatedEntity.LogicalName.ToLower() == "systemuserid") && !UserExists(service, relatedEntity))
                {
                    context.InputParameters.Remove("Target");
                    relatedEntities.Clear();
                }
        }
  }

Leave a Reply

Your email address will not be published. Required fields are marked *