public void Execute(IServiceProvider serviceProvider)
        {
            #region Plugin Declaration
            IPluginExecutionContext     context        = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory        = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService        Service        = factory.CreateOrganizationService(context.UserId);
            ITracingService             tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            #endregion

            try
            {
                // Get the Entity Reference as the Target
                string          relationshipName;
                EntityReference target = (EntityReference)context.InputParameters["Target"];
                if (context.MessageName.ToLower() == "associate" && (target.LogicalName == "new_contracting" || target.LogicalName == "contact"))
                {
                    EntityCollection Result = Service.RetrieveMultiple(Fetches.GetCustomer(target.Id));

                    if (!context.InputParameters.Contains("Relationship"))
                    {
                        return;
                    }
                    // Get the Relationship name for which this plugin fired
                    relationshipName = ((Relationship)context.InputParameters["Relationship"]).SchemaName;
                    if (relationshipName != "new_new_contracting_contact")
                    {
                        return;
                    }
                    // Get the ralationships in the context
                    Relationship relationShip = (Relationship)context.InputParameters["Relationship"];

                    // Get Related Entities in the context
                    EntityReferenceCollection relatedentities = (EntityReferenceCollection)context.InputParameters["RelatedEntities"];
                    foreach (EntityReference relatedEntity in relatedentities)
                    {
                        if (relatedEntity.Id == ((EntityReference)Result.Entities[0].Attributes["new_customer"]).Id)
                        {
                            throw new InvalidPluginExecutionException("This customer is the main sale customer can't be added again,please check!");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Пример #2
0
        public void Execute(IServiceProvider serviceProvider)

        {
            #region Plugin Declaration
            IPluginExecutionContext     context        = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory        = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService        Service        = factory.CreateOrganizationService(context.UserId);
            ITracingService             tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            #endregion

            try
            {
                Entity       target = (Entity)context.InputParameters["Target"];
                Relationship RelationSalesCustomers = new Relationship("new_new_contracting_contact");
                EntityReferenceCollection Customers = new EntityReferenceCollection();

                if (target.LogicalName == "new_contracting" && context.InputParameters["Target"] is Entity)
                {
                    EntityCollection Result = Service.RetrieveMultiple(Fetches.GetCustomers(target.Id));

                    if (Result.Entities.Count > 0)
                    {
                        foreach (Entity customer in Result.Entities)
                        {
                            Customers.Add(new EntityReference("contact", (Guid)customer.Attributes["contactid"]));
                        }
                        Service.Disassociate("new_contracting", (Guid)target.Id, RelationSalesCustomers, Customers);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                tracingService.Trace(ex.ToString());
            }
        }