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

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService    service = localContext.OrganizationService;
            ITracingService         trace   = localContext.TracingService;

            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;


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

            if (postImageEntity.LogicalName != "lead")
            {
                return;
            }

            string message = context.MessageName;

            if (context.Mode == 0) //synchronous plugin
            {
                try
                {
                    ProspectInquiryHandler prospectInquiryHandler = new ProspectInquiryHandler(service, trace);

                    var preImageBaseModelId = preImageEntity.Contains("gsc_vehiclebasemodelid")
                         ? preImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid").Id
                         : Guid.Empty;

                    var preImageDisqualified = preImageEntity.GetAttributeValue <Boolean>("gsc_disqualified");

                    var preImageDisqualifiedStatusReason = preImageEntity.Contains("gsc_disqualifiedstatusreason")
                        ? preImageEntity.GetAttributeValue <Int32>("gsc_disqualifiedstatusreason")
                        : 0;

                    var preImageQualified = preImageEntity.GetAttributeValue <Boolean>("gsc_qualified");

                    var preImageCityName = preImageEntity.Contains("gsc_cityname") ? preImageEntity.GetAttributeValue <string>("gsc_cityname")
                        : String.Empty;

                    var preImageCityIdName = preImageEntity.Contains("gsc_cityid") ? preImageEntity.GetAttributeValue <EntityReference>("gsc_cityid").Name
                        : String.Empty;



                    var postImageBaseModelId = postImageEntity.Contains("gsc_vehiclebasemodelid")
                        ? postImageEntity.GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid").Id
                        : Guid.Empty;

                    var postImageDisqualified = postImageEntity.GetAttributeValue <Boolean>("gsc_disqualified");

                    var postImageDisqualifiedStatusReason = postImageEntity.Contains("gsc_disqualifiedstatusreason")
                        ? postImageEntity.GetAttributeValue <Int32>("gsc_disqualifiedstatusreason")
                        : 0;

                    var postImageQualified = postImageEntity.GetAttributeValue <Boolean>("gsc_qualified");

                    var postImageCityName = postImageEntity.Contains("gsc_cityname") ? postImageEntity.GetAttributeValue <string>("gsc_cityname")
                        : String.Empty;

                    var postImageCityIdName = postImageEntity.Contains("gsc_cityid") ? postImageEntity.GetAttributeValue <EntityReference>("gsc_cityid").Name
                        : String.Empty;

                    //execute the method when Base Model Id was changed
                    if (preImageBaseModelId != postImageBaseModelId)
                    {
                        prospectInquiryHandler.ConcatenateVehicleInfo(postImageEntity, message);
                    }

                    //Call DisqualifyProspectInquiry method on Disqualified and Disqualified Status Reason change
                    if (preImageDisqualified != postImageDisqualified && preImageDisqualifiedStatusReason != postImageDisqualifiedStatusReason)
                    {
                        prospectInquiryHandler.DisqualifyProspectInquiry(postImageEntity);
                    }
                    //Call QualifyProsctInquiry
                    if (preImageQualified != postImageQualified)
                    {
                        prospectInquiryHandler.CreateCustomer(postImageEntity);
                        prospectInquiryHandler.CreateOpportunity(postImageEntity);
                        prospectInquiryHandler.QualifyProspectInquiry(postImageEntity);
                    }

                    if (postImageCityName != postImageCityIdName && postImageCityName != String.Empty && preImageCityIdName == postImageCityIdName)
                    {
                        prospectInquiryHandler.SetCity(postImageEntity);
                    }
                }
                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException(String.Concat(ex.Message));
                }
            }
        }
Example #2
0
        public void ConcatenateVehicleInfoWithNullValues()
        {
            #region 1. Setup / Arrange
            var orgServiceMock = new Mock <IOrganizationService>();
            var orgService     = orgServiceMock.Object;
            var orgTracingMock = new Mock <ITracingService>();
            var orgTracing     = orgTracingMock.Object;

            #region Vehicle Base Model Entity
            var VehicleBaseModelCollection = new EntityCollection()
            {
                EntityName = "gsc_iv_vehiclebasemodel",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_iv_vehiclebasemodel",
                        Attributes  =
                        {
                            { "gsc_basemodelpn", "Strada" }
                        }
                    }
                }
            };
            #endregion

            #region ProspectInquiry EntityCollection
            var ProspectInquiryCollection = new EntityCollection
            {
                EntityName = "lead",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "lead",
                        EntityState = EntityState.Changed,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_vehiclebasemodelid", new EntityReference() },
                            { "fullname",               ""                    },
                            { "topic",                  ""                    },
                        }
                    }
                }
            };
            #endregion

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == VehicleBaseModelCollection.EntityName)
                                      ))).Returns(VehicleBaseModelCollection);

            #endregion

            #region 2. Call/Action

            var    ProspectInquiryHandler = new ProspectInquiryHandler(orgService, orgTracing);
            String Subject = ProspectInquiryHandler.ConcatenateVehicleInfo(ProspectInquiryCollection.Entities[0], "Create");

            #endregion

            #region 3. Verify
            var vehiclemodel = VehicleBaseModelCollection.Entities[0]["gsc_basemodelpn"];
            var fullname     = ProspectInquiryCollection.Entities[0]["fullname"];
            var topic        = fullname + " - " + vehiclemodel;
            Assert.AreEqual(topic, Subject);

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

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

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

            if (prospectInquiry.LogicalName != "lead")
            {
                return;
            }

            string message = context.MessageName;

            if (context.Mode == 0) //synchronous plugin
            {
                try
                {
                    ProspectInquiryHandler prospectInquiryHandler = new ProspectInquiryHandler(service, trace);
                    prospectInquiryHandler.GetDefaultTax();
                    prospectInquiryHandler.ReplicateProspectInfo(prospectInquiry);
                    prospectInquiryHandler.ConcatenateVehicleInfo(prospectInquiry, message);
                    //set Portal User Id
                    prospectInquiry["gsc_portaluserid"] = prospectInquiry.GetAttributeValue <EntityReference>("gsc_recordownerid") != null
                        ? prospectInquiry.GetAttributeValue <EntityReference>("gsc_recordownerid").Id.ToString()
                        : String.Empty;

                    //Set Name for corporate
                    var prospectType = prospectInquiry.Contains("gsc_prospecttype") ? prospectInquiry.GetAttributeValue <OptionSetValue>("gsc_prospecttype").Value : 0;
                    if (prospectType != 100000000)
                    {
                        prospectInquiry["gsc_prospectname"] = prospectInquiry.GetAttributeValue <String>("companyname") != null
                        ? prospectInquiry.GetAttributeValue <String>("companyname")
                        : String.Empty;
                    }
                    else
                    {
                        prospectInquiry["gsc_prospectname"] = prospectInquiry.GetAttributeValue <String>("fullname") != null
                        ? prospectInquiry.GetAttributeValue <String>("fullname")
                        : String.Empty;
                    }

                    if (prospectInquiryHandler.CheckForExistingRecords(prospectInquiry) == true)
                    {
                        throw new InvalidPluginExecutionException("This record has been identified as a fraud account. Please ask the customer to provide further information.");
                    }
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("This record has been identified as a fraud account. Please ask the customer to provide further information."))
                    {
                        throw new InvalidPluginExecutionException("This record has been identified as a fraud account. Please ask the customer to provide further information.");
                    }
                    else
                    {
                        throw new InvalidPluginExecutionException(ex.Message);
                    }
                }
            }
        }