/// <summary>
        /// Executes the plug-in.
        /// </summary>
        /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>
        /// and <see cref="ITracingService"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
        /// The plug-in's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the plug-in. Also, multiple system threads
        /// could execute the plug-in at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in plug-ins.
        /// </remarks>
        protected void ExecutePreValidateRequirementChecklistDelete(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService    service = localContext.OrganizationService;
            ITracingService         trace   = localContext.TracingService;
            var    requirementChecklist     = (EntityReference)context.InputParameters["Target"];
            string message = context.MessageName;

            if (context.Depth > 1)
            {
                return;
            }

            try
            {
                EntityCollection requirementChecklistCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_sls_requirementchecklist", "gsc_sls_requirementchecklistid", requirementChecklist.Id, service, null, OrderType.Ascending,
                                                                                                          new[] { "gsc_requirementchecklistpn", "gsc_predefined" });

                RequirementChecklistHandler requirementChecklistHandler = new RequirementChecklistHandler(service, trace);

                requirementChecklistHandler.ValidateDelete(requirementChecklistCollection.Entities[0], message);
            }

            catch (Exception ex)
            {
                if (ex.Message.Contains("Unable to delete required document."))
                {
                    throw new InvalidPluginExecutionException("Unable to delete required document.");
                }
                else
                {
                    throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace));
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Executes the plug-in.
        /// </summary>
        /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>
        /// and <see cref="ITracingService"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
        /// The plug-in's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the plug-in. Also, multiple system threads
        /// could execute the plug-in at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in plug-ins.
        /// </remarks>
        protected void ExecutePostRequirementChecklistUpdate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;

            Entity preImageEntity  = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;
            Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;

            IOrganizationService service = localContext.OrganizationService;
            ITracingService      trace   = localContext.TracingService;

            if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity))
            {
                return;
            }

            Entity requirementChecklistEntity = (Entity)context.InputParameters["Target"];

            if (requirementChecklistEntity.LogicalName != "gsc_sls_requirementchecklist")
            {
                return;
            }

            if (context.Mode == 0) //synchronous plugin
            {
                try
                {
                    string message = context.MessageName;

                    RequirementChecklistHandler requirementChecklistHandler = new RequirementChecklistHandler(service, trace);

                    var preImageSubmitted = preImageEntity.Contains("gsc_submitted")
                        ? preImageEntity.GetAttributeValue <bool>("gsc_submitted")
                        : false;
                    var preImageDocumentType = preImageEntity.Contains("gsc_documenttype")
                        ? preImageEntity.GetAttributeValue <bool>("gsc_documenttype")
                        : false;
                    var preImageDocumentName = preImageEntity.Contains("gsc_documentchecklistid")
                        ? preImageEntity.GetAttributeValue <EntityReference>("gsc_documentchecklistid").Id
                        : Guid.Empty;


                    var postImageSubmitted = postImageEntity.Contains("gsc_submitted")
                        ? postImageEntity.GetAttributeValue <bool>("gsc_submitted")
                        : false;
                    var postImageDocumentType = postImageEntity.Contains("gsc_documenttype")
                        ? postImageEntity.GetAttributeValue <bool>("gsc_documenttype")
                        : false;
                    var postImageDocumentName = postImageEntity.Contains("gsc_documentchecklistid") ?
                                                postImageEntity.GetAttributeValue <EntityReference>("gsc_documentchecklistid").Id
                        : Guid.Empty;

                    if (preImageSubmitted != postImageSubmitted && postImageSubmitted == true)
                    {
                        requirementChecklistHandler.SetForAllocation(postImageEntity);
                    }

                    if (preImageDocumentType != postImageDocumentType || preImageDocumentName != postImageDocumentName)
                    {
                        requirementChecklistHandler.ValidateEdit(postImageEntity);
                    }
                }

                catch (Exception ex)
                {
                    if (ex.Message.Contains("Unable to modify documents from bank."))
                    {
                        throw new InvalidPluginExecutionException("Unable to modify documents from bank.");
                    }
                    else
                    {
                        throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace));
                    }
                }
            }
        }