Friday 2 November 2012

Creating a plug-in to assign a record in Microsoft Dynamics CRM 2011 using Early Bound Entities.


In this post I will show you how to develop a plug-in to assign records related to an entity over a 1:N relationship.
Let’s put some background. Now imagine we have a child entity “Child” related to a parent entity “Parent” (N:1). When we assign “Parent” to another user, we also want that all the related records of type “Child” to be assigned to that user. You can do so by configuring the relationship behavior to “Parental” for the relationship between “Parent” and “Child”.
Ok, so far, so good, but what if “Child” also has a “Parental” relationship with another entity? We just can’t do it. CRM only allows one parental relationship between two entities (Out-of-the-box Dynamics CRM comes with several relationships of type “Parental” for the same entity, but with custom ones, it’s not possible).
So, the only way to carry out this task is through a plug-in, registered for the “Assign” message on the parent entity.
This is the code for it:
public void Execute(IServiceProvider serviceProvider)
{
//Grab the plugin execution context
IPluginExecutionContext Context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

//Grab the service factory
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

//Create the organization service
IOrganizationService Service = factory.CreateOrganizationService(Context.UserId);

if (Context.InputParameters.Contains("Assignee"))    //Parameter of the Assign request
{
    EntityReference assignee = (EntityReference)Context.InputParameters["Assignee"];
    //Data Context from the crmsvcutil.exe generated class with option //serviceContextName:XRMDataService
    using (XRMDataService DataContext = new XRMDataService(Service))
       {
           Guid ParentEntityId = Context.PrimaryEntityId;
               new_parent Parent = DataContext.new_parentSet.Where
                  (c => c.new_parentId == ParentEntityId).FirstOrDefault();
            
          if (Parent != null)
          {
                //Load the related entity collection for the relationship
                DataContext.LoadProperty(Parent, new_new_parent_new_child");
                   
             if (Parent. new_new_parent_new_child != null)
             {
                 try
                {
                        List<new_child> AssociatedChilds = DataContext.new_childSet.Where
                         (a => a.new_parentId == Parent.ToEntityReference()).ToList();
                        if (AssociatedChilds != null)
                   {
                           foreach (Child Childs in AssociatedChilds)
                      {
                          AssignRequest assign = new AssignRequest
                        {
                            // User or team assigned to the child records
                                Assignee = assignee,
                            //Target child record
                            Target = Childs.ToEntityReference()
                        };
                        // Execute the Request
                        Service.Execute(assign);                                        
                      }
                   }
                }
                catch (Exception ex)
                {
                        throw new InvalidPluginExecutionException(
                   string.Format("An error occurred in the plug-in." + ex.ToString());                           
                }
             }
             }   
          } //End of using DataContext
    }
}

No comments:

Post a Comment