public void CreateOpportunity()
        {

            #region 1. Setup / Arrange
            var orgServiceMock = new Mock<IOrganizationService>();
            var orgService = orgServiceMock.Object;
            var orgTracingMock = new Mock<ITracingService>();
            var orgTracing = orgTracingMock.Object;

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

            #endregion

            #region 2. Call/Action
            var ProspectInquiryHandler = new ProspectInquiryHandler(orgService, orgTracing);
            Entity OpportunityEntity = ProspectInquiryHandler.CreateOpportunity(ProspectInquiryCollection.Entities[0]);
            #endregion

            #region 3. Verify
            Assert.AreEqual(ProspectInquiryCollection.Entities[0].Id, OpportunityEntity.GetAttributeValue<EntityReference>("originatinglead").Id);
            #endregion
        }
Ejemplo 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 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));
                }
            }
        }