Example #1
0
        public Boolean AlterarStatus(Guid id, int stateCode, int statusCode)
        {
            if ((int)Domain.Enum.Pedido.StateCode.Cancelada == stateCode)
            {
                CancelSalesOrderResponse cancelResponse = null;
                Entity cancelaPedido = new Entity("orderclose");
                cancelaPedido["salesorderid"] = new EntityReference(SDKore.Crm.Util.Utility.GetEntityName <Intelbras.CRM2013.Domain.Model.Pedido>(), id);

                CancelSalesOrderRequest requestCancel = new CancelSalesOrderRequest();

                requestCancel.OrderClose = cancelaPedido;
                requestCancel.Status     = new OptionSetValue(statusCode);
                cancelResponse           = (CancelSalesOrderResponse)this.Execute(requestCancel);
                if (cancelResponse != null)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                SetStateResponse resp = null;

                SetStateRequest request = new SetStateRequest
                {
                    EntityMoniker = new EntityReference("salesorder", id),
                    State         = new OptionSetValue(stateCode),
                    Status        = new OptionSetValue(statusCode)
                };

                resp = (SetStateResponse)this.Execute(request);


                if (resp != null)
                {
                    return(true);
                }
            }
            return(false);
        }
        /// <summary>
        /// Cancel Sales order
        /// </summary>
        /// <param name="salesOrderId">Sales order id to close</param>
        /// <param name="fieldList">List of fields to add</param>
        /// <param name="orderStatusCode">Status code of the order</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 CancelSalesOrder(this CdsServiceClient cdsServiceClient, Guid salesOrderId, Dictionary <string, CdsDataTypeWrapper> fieldList, int orderStatusCode = 4, 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 (salesOrderId == Guid.Empty)
            {
                return(Guid.Empty);
            }

            if (orderStatusCode < 4)
            {
                return(Guid.Empty);
            }

            Guid   actId = Guid.Empty;
            Entity uEnt  = new Entity("orderclose");
            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("salesorderid"))
            {
                PropertyList.Add(new KeyValuePair <string, object>("salesorderid", salesOrderId));
            }

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

            CancelSalesOrderRequest req = new CancelSalesOrderRequest();
            req.OrderClose = uEnt;
            req.Status     = new OptionSetValue(orderStatusCode);

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


            CancelSalesOrderResponse resp = (CancelSalesOrderResponse)cdsServiceClient.CdsCommand_Execute(req, "Closing a Sales Order in CRM as Closed", bypassPluginExecution);
            if (resp != null)
            {
                return(actId);
            }
            else
            {
                return(Guid.Empty);
            }
        }