/// <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> /// <returns></returns> public static Guid CloseOpportunity(this CdsServiceClient cdsServiceClient, Guid opportunityId, Dictionary <string, CdsDataTypeWrapper> fieldList, int opportunityStatusCode = 3, Guid batchId = default(Guid)) { 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")) { return(Guid.Empty); } WinOpportunityResponse resp = (WinOpportunityResponse)cdsServiceClient.CdsCommand_Execute(req, "Closing a Opportunity in CRM as Won"); 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")) { return(Guid.Empty); } LoseOpportunityResponse resp = (LoseOpportunityResponse)cdsServiceClient.CdsCommand_Execute(req, "Closing a Opportunity in CRM as Lost"); if (resp != null) { return(actId); } else { return(Guid.Empty); } } }