Beispiel #1
0
        public static MsCrmResult CloseOppAsLost(Guid oppId, int statusCode, DateTime actualEnd, IOrganizationService service)
        {
            MsCrmResult returnValue = new MsCrmResult();

            try
            {
                if (oppId != Guid.Empty)
                {
                    LoseOpportunityRequest req = new LoseOpportunityRequest();
                    Entity opportunityClose    = new Entity("opportunityclose");
                    opportunityClose["opportunityid"] = new EntityReference("opportunity", oppId);
                    opportunityClose["subject"]       = "Lost the Opportunity!";
                    opportunityClose["actualend"]     = actualEnd;

                    req.OpportunityClose = opportunityClose;
                    req.Status           = new OptionSetValue(statusCode); // Durum açıklaması "Kaybedildi"
                    LoseOpportunityResponse resp = (LoseOpportunityResponse)service.Execute(req);
                    returnValue.Success = true;
                    returnValue.Result  = "Fırsat kaybedildi olarak kapatıldı.";
                }
            }
            catch (Exception ex)
            {
                returnValue.Success = false;
                returnValue.Result  = "Beklenmedik bir hata ile karşılaşıldı. Hata = " + ex.Message;
            }

            return(returnValue);
        }
Beispiel #2
0
        private LoseOpportunityResponse HandleLoseOpportunity(OrganizationRequest orgRequest, EntityReference userRef)
        {
            var request = MakeRequest <LoseOpportunityRequest>(orgRequest);
            var resp    = new LoseOpportunityResponse();

            dataMethods.CloseOpportunity(OpportunityState.Lost, request.Status, request.OpportunityClose, userRef);
            return(resp);
        }
        /// <summary>
        /// This will close an opportunity as either Won or lost in CRM
        /// </summary>
        /// <param name="opportunityId">ID of the opportunity to close</param>
        /// <param name="fieldList">List of fields for the Opportunity Close Entity</param>
        /// <param name="opportunityStatusCode">Status code of Opportunity, Should be either 1 or 2,  defaults to 1 ( won )</param>
        /// <param name="batchId">Optional: if set to a valid GUID, generated by the Create Batch Request Method, will assigned the request to the batch for later execution, on fail, runs the request immediately </param>
        /// <param name="cdsServiceClient">Connected CDS Service Client</param>
        /// <param name="bypassPluginExecution">Adds the bypass plugin behavior to this request. Note: this will only apply if the caller has the prvBypassPlugins permission to bypass plugins.  If its attempted without the permission the request will fault.</param>
        /// <returns></returns>
        public static Guid CloseOpportunity(this CdsServiceClient cdsServiceClient, Guid opportunityId, Dictionary <string, CdsDataTypeWrapper> fieldList, int opportunityStatusCode = 3, Guid batchId = default(Guid), bool bypassPluginExecution = false)
        {
            cdsServiceClient.logEntry.ResetLastError();              // Reset Last Error
            if (cdsServiceClient._CdsService == null)
            {
                cdsServiceClient.logEntry.Log("Crm Service not initialized", TraceEventType.Error);
                return(Guid.Empty);
            }

            if (opportunityId == Guid.Empty)
            {
                return(Guid.Empty);
            }

            if (opportunityStatusCode < 3)
            {
                return(Guid.Empty);
            }

            Guid   actId = Guid.Empty;
            Entity uEnt  = new Entity("opportunityclose");
            AttributeCollection PropertyList = new AttributeCollection();

            #region MapCode
            if (fieldList != null)
            {
                foreach (KeyValuePair <string, CdsDataTypeWrapper> field in fieldList)
                {
                    cdsServiceClient.AddValueToPropertyList(field, PropertyList);
                }
            }

            // Add the key...
            // check to see if the key is in the import set allready
            if (fieldList != null && !fieldList.ContainsKey("opportunityid"))
            {
                PropertyList.Add(new KeyValuePair <string, object>("opportunityid", opportunityId));
            }

            if (fieldList != null && fieldList.ContainsKey("activityid"))
            {
                actId = (Guid)fieldList["activityid"].Value;
            }
            else
            {
                actId   = Guid.NewGuid();
                uEnt.Id = actId;
            }
            #endregion
            uEnt.Attributes.AddRange(PropertyList.ToArray());

            // 2 types of close supported... Won or Lost.
            if (opportunityStatusCode == 3)
            {
                WinOpportunityRequest req = new WinOpportunityRequest();
                req.OpportunityClose = uEnt;
                req.Status           = new OptionSetValue(opportunityStatusCode);

                if (cdsServiceClient.AddRequestToBatch(batchId, req, "Calling Close Opportunity as Won", "Request to Close Opportunity as Won Queued", bypassPluginExecution))
                {
                    return(Guid.Empty);
                }

                WinOpportunityResponse resp = (WinOpportunityResponse)cdsServiceClient.CdsCommand_Execute(req, "Closing a Opportunity in CRM as Won", bypassPluginExecution);
                if (resp != null)
                {
                    return(actId);
                }
                else
                {
                    return(Guid.Empty);
                }
            }
            else
            {
                LoseOpportunityRequest req = new LoseOpportunityRequest();
                req.OpportunityClose = uEnt;
                req.Status           = new OptionSetValue(opportunityStatusCode);

                if (cdsServiceClient.AddRequestToBatch(batchId, req, "Calling Close Opportunity as Lost", "Request to Close Opportunity as Lost Queued", bypassPluginExecution))
                {
                    return(Guid.Empty);
                }

                LoseOpportunityResponse resp = (LoseOpportunityResponse)cdsServiceClient.CdsCommand_Execute(req, "Closing a Opportunity in CRM as Lost", bypassPluginExecution);
                if (resp != null)
                {
                    return(actId);
                }
                else
                {
                    return(Guid.Empty);
                }
            }
        }