locked
Plugin: Set the Tax and the ExtendedAmount in SalesOrderDetail RRS feed

  • Question

  •  Hello People.

    I am trying to set the Tax and the ExtendedAmount in SalesOrderDetail, using a Plugin in the Post Event. In CRM 2011.

    But, the Tax nor the ExtendedAmount are not shown.

    When I use this line,  it does not take effect and the values are not shown:

                                             service.Update(pedido);

    The complete code is the following:

    public void Execute(IServiceProvider serviceProvider)

                {

                    // Obtain the execution context from the service provider.

                   

                    IPluginExecutionContext context = (IPluginExecutionContext)

                    serviceProvider.GetService(typeof(IPluginExecutionContext));

                    if (esEntidadPedido(context))

                    {

                        try

                        {

                            IOrganizationServiceFactory serviceFactory =

                            (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

     

                            _service = service;

     

                            OrganizationServiceContext ServiceContext = new OrganizationServiceContext(service);

     

                            Entity entity = (Entity)context.InputParameters["Target"];

                            ColumnSet columnas = new ColumnSet(true);

                            Entity entityProductoPedido = service.Retrieve(entity.LogicalName, entity.Id, columnas);

     

                            //registro del detalle

                            SalesOrderDetail productoPedidoDetalle = (SalesOrderDetail)entityProductoPedido;

                            if (productoPedidoDetalle != null)

                            {

                                //guid = productoPedidoDetalle.SalesOrderDetailId.Value;

                                   

                                PrecioUnit = productoPedidoDetalle.PricePerUnit;

                                int selectValue = productoPedidoDetalle.mnt_IVA.Value;

                                // Obtengo el valor del iva, si 0% O 12%

                                var iva = GetOptionsSetTextOnValue(_service,"salesorderdetail","mnt_iva",selectValue);

     

                                PorcentIva = Convert.ToDecimal(iva);

                                Cantidad = productoPedidoDetalle.Quantity.Value;

                                DescVol = productoPedidoDetalle.VolumeDiscountAmount;

                                DescOtros = productoPedidoDetalle.ManualDiscountAmount;

                              

                                if (PorcentIva != null)

                                {

                                    if (PorcentIva > 0)

                                    {  

                                        int Division = 100;

     

                                        // Obtengo el Subtotal

                                        if (DescVol != null)

                                        {

                                            Subtotal = (int)Cantidad * (PrecioUnit.Value - DescVol.Value);

                                        }

                                        else

                                        {

                                            Subtotal = (int)Cantidad * PrecioUnit.Value;

                                        }

                                        // Si descuento Manual es Distincto de NUll entra por aqui

                                        if (DescOtros != null)

                                        {

                                            if (DescOtros.Value > 0)

                                            {

                                                ValorIVa = (int)((Subtotal - DescOtros.Value) * PorcentIva) / Division;

                                            }

                                            else

                                            {

                                                ValorIVa = (int)(Subtotal * PorcentIva) / Division;

                                            }

                                        }

                                        else // Si descuento Manual es Null entra por aqui.

                                        {

                                            ValorIVa = (int)(Subtotal * PorcentIva) / Division;

     

                                        }

                                    }

                                    else

                                    { 

                                        // Obtengo el Subtotal

                                        if (DescVol != null)

                                        {

                                            Subtotal = (int) Cantidad * (PrecioUnit.Value - DescVol.Value);

                                            ValorIVa = 0;

                                        }

                                        else

                                        {

                                            Subtotal = (int)Cantidad * PrecioUnit.Value;

                                            ValorIVa = 0;

                                        }

                                    }

                                    SalesOrderDetail pedido = new SalesOrderDetail();

                                                                   

                                    pedido.SalesOrderId = productoPedidoDetalle.SalesOrderId;

                                    pedido.SalesOrderDetailId = productoPedidoDetalle.SalesOrderDetailId;

                                    pedido.ProductId = productoPedidoDetalle.ProductId;

                                   

                                    Money impuesto = new Money(ValorIVa);

                                    pedido.Tax = impuesto;

                                    service.Update(pedido);

                                 

                                }

                            }

                        }

                        catch (FaultException<OrganizationServiceFault> ex)

                        {

                            throw new InvalidPluginExecutionException("Error en el plug-in." + ex.Message + " - " + ex.StackTrace);

                        }

                        catch (Exception ex)

                        {

                            throw new InvalidPluginExecutionException("Error General en el plug-in." + ex.Message + " - " + ex.StackTrace);

                        }

                    }

                }


    Roxana Cevallos Q. -PARTNER-

    Thursday, October 11, 2012 3:06 PM

Answers

  • Hi,

    It seems you are using custom logic to calculate extendedamount, but MS CRM have it own logic to calculate extendedamount so it will over write your extendedamount based on system logic. you need to create custom field to store extended amount based on your custom logic.


    Contact Me
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Thursday, October 11, 2012 3:58 PM
    Moderator
  • Hi, thanks

    Ok. I used a custom field, where I set the "Calculated Tax", then I set the "tax" field (system field, which is not locked).

    The amountextended (systemfield) is calculated automatically and already has my tax :D

    SalesOrderDetail pedido = new SalesOrderDetail();

    pedido.SalesOrderId = productoPedidoDetalle.SalesOrderId;

    pedido.Id = productoPedidoDetalle.SalesOrderDetailId.Value;

    Money impuesto = new Money(ValorIVa);

    pedido.Tax = impuesto;

    //pedido.ExtendedAmount.Value = Total.Value;

    service.Update(pedido);


    Roxana Cevallos Q. -PARTNER-


    Thursday, October 11, 2012 5:57 PM

All replies

  • Hi,

    It seems you are using custom logic to calculate extendedamount, but MS CRM have it own logic to calculate extendedamount so it will over write your extendedamount based on system logic. you need to create custom field to store extended amount based on your custom logic.


    Contact Me
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Thursday, October 11, 2012 3:58 PM
    Moderator
  • Hi, thanks

    Ok. I used a custom field, where I set the "Calculated Tax", then I set the "tax" field (system field, which is not locked).

    The amountextended (systemfield) is calculated automatically and already has my tax :D

    SalesOrderDetail pedido = new SalesOrderDetail();

    pedido.SalesOrderId = productoPedidoDetalle.SalesOrderId;

    pedido.Id = productoPedidoDetalle.SalesOrderDetailId.Value;

    Money impuesto = new Money(ValorIVa);

    pedido.Tax = impuesto;

    //pedido.ExtendedAmount.Value = Total.Value;

    service.Update(pedido);


    Roxana Cevallos Q. -PARTNER-


    Thursday, October 11, 2012 5:57 PM