/// <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 ExecutePostQuoteCabChassisCreate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }


            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService    service = localContext.OrganizationService;
            ITracingService         trace   = localContext.TracingService;
            Entity quoteCabChassis          = (Entity)context.InputParameters["Target"];

            string message = context.MessageName;
            string error   = "";

            try
            {
                QuoteCabChassisHandler quoteCCHandler = new QuoteCabChassisHandler(service, trace);
                quoteCCHandler.SetCCAddOnAmount(quoteCabChassis, message);
            }
            catch (Exception ex)
            {
                //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error));
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
Exemple #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 ExecutePreValidateQuoteCabChassisDelete(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

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

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

            try
            {
                QuoteCabChassisHandler quoteCabChassisHandler = new QuoteCabChassisHandler(service, trace);

                EntityCollection quoteCabChassisCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_sls_quotecabchassis", "gsc_sls_quotecabchassisid", quoteCabChassisEntity.Id, service, null, OrderType.Ascending,
                                                                                                     new[] { "gsc_quoteid", "gsc_amount", "gsc_financing" });

                if (quoteCabChassisCollection != null && quoteCabChassisCollection.Entities.Count > 0)
                {
                    Entity quoteCabChassis = quoteCabChassisCollection.Entities[0];
                    quoteCabChassisHandler.SetCCAddOnAmount(quoteCabChassis, message);
                }
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error));
            }
        }
        /// <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 ExecutePostQuoteCabChassisUpdate(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 quoteCC = (Entity)context.InputParameters["Target"];

            if (quoteCC.LogicalName != "gsc_sls_quotecabchassis")
            {
                return;
            }

            if (context.Mode == 0) //Synchronous Plugin
            {
                try
                {
                    QuoteCabChassisHandler quoteCCHandler = new QuoteCabChassisHandler(service, trace);

                    string message = context.MessageName;

                    var  preAmount    = preImageEntity.Contains("gsc_amount") ? preImageEntity.GetAttributeValue <Money>("gsc_amount").Value : 0;
                    var  preFinancing = preImageEntity.GetAttributeValue <bool>("gsc_financing");
                    Guid preImageItem = preImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclecabchassisid") != null
                        ? preImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclecabchassisid").Id
                        : Guid.Empty;

                    var  postAmount    = postImageEntity.Contains("gsc_amount") ? postImageEntity.GetAttributeValue <Money>("gsc_amount").Value : 0;
                    var  postFinancing = postImageEntity.GetAttributeValue <bool>("gsc_financing");
                    Guid postImageItem = postImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclecabchassisid") != null
                        ? postImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclecabchassisid").Id
                        : Guid.Empty;

                    if (preAmount != postAmount || preFinancing != postFinancing)
                    {
                        quoteCCHandler.SetCCAddOnAmount(postImageEntity, message);
                    }

                    if (preImageItem != postImageItem)
                    {
                        quoteCCHandler.PopulateDetails(postImageEntity, message);
                    }
                }

                catch (Exception ex)
                {
                    //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error));
                    throw new InvalidPluginExecutionException(ex.Message);
                }
            }
        }