D365插件错误来自ISV代码出乎意料的错误

发布于 2025-02-03 03:34:29 字数 4974 浏览 1 评论 0原文

您好,世界上的编码器,

我正在编写我的第一个D365插件,并且遇到了一个问题。当我保存表格时,什么也不会发生。

我在报价产品实体上注册了用于计算“增值税金额”的插件(更新消息,后操作)。我有一个相关的“增值税”实体,其中包含一个整数。该插件检索该整数并变成十进制(代表一个百分比)。增值税的数量是通过将该小数乘以引用产品中的数量来计算的:

decimal amountVatExcluded = baseAmount - discount;
decimal vatAmount = amountVatExcluded * vatPercentage;
decimal amountVatIncluded = amountVatExcluded + vatAmount;

当我调试插件时,在第41行中丢弃了错误,

 decimal vatPercentage = GetVatPercentage(quoteLine, service);

该错误通过从方法getVarperCentage中撤回小数来声明变量:

private decimal GetVatPercentage(Entity quoteLine, IOrganizationService service)
        {
            Guid vatId = (Guid)quoteLine.Attributes["capdcx_valueaddedtax"];
            ColumnSet attributesVatEntity = new ColumnSet("capdcx_valueaddedtaxpercentage");

            Entity vatEntity = service.Retrieve("capdcx_valueaddedtax", vatId, attributesVatEntity);
         

            decimal vatPercentageAsWholeNumber = 0;
            if (vatEntity.Attributes.Contains("capdcx_valueaddedtaxpercentage"))
            {
                vatPercentageAsWholeNumber = (decimal)vatEntity.Attributes["capdcx_valueaddedtaxpercentage"];
            }
            
            decimal vatPercentage = vatPercentageAsWholeNumber / 100;
            return vatPercentage;
        }

我无法对vatpercentage十进制的检索错误。有什么想法吗? Bewlow是完整的代码:

public void Execute(IServiceProvider serviceProvider)
        {
            #region pluginbase
            ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));

            IOrganizationServiceFactory serviceFactory =
                (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            #endregion

            #region attributes
            tracing.Trace("Obtaining Target (quoteLine) entity and attributes");
            
            Entity quoteLine = (Entity)context.InputParameters["Target"];
            ColumnSet attributesQuoteline = new ColumnSet(new string[]
            {
                "baseamount",
                "manualdiscountamount",
                "capdcx_valueaddedtax"
            });

            service.Retrieve("quotedetail", quoteLine.Id, attributesQuoteline);
            #endregion

            //access related VAT entity, get its integer and turn it into decimal for percentage
            decimal vatPercentage = GetVatPercentage(quoteLine, service);

            //define price, discount, and final price without VAT
            decimal baseAmount = 0;
            if (quoteLine.Attributes.Contains("baseamount"))
            {
                baseAmount = (decimal)quoteLine.Attributes["baseamount"];
            }

            decimal discount = 0; 
            if (quoteLine.Attributes.Contains("manualdiscountamount"))
            {
                discount = (decimal)quoteLine.Attributes["manualdiscountamount"];
            }


            decimal amountVatExcluded = baseAmount - discount;
            decimal vatAmount = amountVatExcluded * vatPercentage;
            decimal amountVatIncluded = amountVatExcluded + vatAmount;

            var quotelineUpdated = new Entity(quoteLine.LogicalName, quoteLine.Id);
            quotelineUpdated.Attributes["capdcx_valueaddedtaxexcluded"] = new Money(amountVatExcluded);
            quotelineUpdated.Attributes["capdcx_valueaddedtax"] = new Money(vatAmount);
            quotelineUpdated.Attributes["extendedamount"] = new Money(amountVatIncluded);

            service.Update(quotelineUpdated);
            tracing.Trace("Succesfully set amounts of quoteLine");
            // onCreate en onSave
        }


        private decimal GetVatPercentage(Entity quoteLine, IOrganizationService service)
        {
            Guid vatId = (Guid)quoteLine["capdcx_valueaddedtax"];
            ColumnSet attributesVatEntity = new ColumnSet(new string[]
            {
                "capdcx_valueaddedtaxpercentage"
            });
            Entity vatEntity = service.Retrieve("capdcx_valueaddedtax", vatId, attributesVatEntity);
            //Entity vatEntity = service.Retrieve("capdcx_valueaddedtax", (Guid)quoteLine["capdcx_valueaddedtax"], new ColumnSet("capdcx_valueaddedtaxpercentage"));

            decimal vatPercentageAsWholeNumber = 0;
            if (vatEntity.Attributes.Contains("capdcx_valueaddedtaxpercentage"))
            {
                vatPercentageAsWholeNumber = (decimal)vatEntity.Attributes["capdcx_valueaddedtaxpercentage"];
            }
            
            decimal vatPercentage = vatPercentageAsWholeNumber / 100;
            return vatPercentage;
        }

非常感谢!

问候, Malliendk

Hello coders of the world,

I'm writing my first d365 plugin and I'm encountering a problem. When I save my form, nothing happens.

I'm registered the plugin on the Quote Product entity for calculating "VAT amount" (Update message, Post Operation). I have a related "VAT" entity, which contains an integer. The plugin retrieves this integer and turns into a decimal (which represents a percentage). The VAT amount is calculated by multiplying this decimal by the amount in Quote Product:

decimal amountVatExcluded = baseAmount - discount;
decimal vatAmount = amountVatExcluded * vatPercentage;
decimal amountVatIncluded = amountVatExcluded + vatAmount;

When I debug the plugin, an error is thrown at line 41,

 decimal vatPercentage = GetVatPercentage(quoteLine, service);

which declares a variable by retrtieving a decimal from a method GetVarPercentage:

private decimal GetVatPercentage(Entity quoteLine, IOrganizationService service)
        {
            Guid vatId = (Guid)quoteLine.Attributes["capdcx_valueaddedtax"];
            ColumnSet attributesVatEntity = new ColumnSet("capdcx_valueaddedtaxpercentage");

            Entity vatEntity = service.Retrieve("capdcx_valueaddedtax", vatId, attributesVatEntity);
         

            decimal vatPercentageAsWholeNumber = 0;
            if (vatEntity.Attributes.Contains("capdcx_valueaddedtaxpercentage"))
            {
                vatPercentageAsWholeNumber = (decimal)vatEntity.Attributes["capdcx_valueaddedtaxpercentage"];
            }
            
            decimal vatPercentage = vatPercentageAsWholeNumber / 100;
            return vatPercentage;
        }

I can't fingure out what's wrong with this retrieval of the VatPercentage decimal. Any thoughts? Bewlow is the full code:

public void Execute(IServiceProvider serviceProvider)
        {
            #region pluginbase
            ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));

            IOrganizationServiceFactory serviceFactory =
                (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            #endregion

            #region attributes
            tracing.Trace("Obtaining Target (quoteLine) entity and attributes");
            
            Entity quoteLine = (Entity)context.InputParameters["Target"];
            ColumnSet attributesQuoteline = new ColumnSet(new string[]
            {
                "baseamount",
                "manualdiscountamount",
                "capdcx_valueaddedtax"
            });

            service.Retrieve("quotedetail", quoteLine.Id, attributesQuoteline);
            #endregion

            //access related VAT entity, get its integer and turn it into decimal for percentage
            decimal vatPercentage = GetVatPercentage(quoteLine, service);

            //define price, discount, and final price without VAT
            decimal baseAmount = 0;
            if (quoteLine.Attributes.Contains("baseamount"))
            {
                baseAmount = (decimal)quoteLine.Attributes["baseamount"];
            }

            decimal discount = 0; 
            if (quoteLine.Attributes.Contains("manualdiscountamount"))
            {
                discount = (decimal)quoteLine.Attributes["manualdiscountamount"];
            }


            decimal amountVatExcluded = baseAmount - discount;
            decimal vatAmount = amountVatExcluded * vatPercentage;
            decimal amountVatIncluded = amountVatExcluded + vatAmount;

            var quotelineUpdated = new Entity(quoteLine.LogicalName, quoteLine.Id);
            quotelineUpdated.Attributes["capdcx_valueaddedtaxexcluded"] = new Money(amountVatExcluded);
            quotelineUpdated.Attributes["capdcx_valueaddedtax"] = new Money(vatAmount);
            quotelineUpdated.Attributes["extendedamount"] = new Money(amountVatIncluded);

            service.Update(quotelineUpdated);
            tracing.Trace("Succesfully set amounts of quoteLine");
            // onCreate en onSave
        }


        private decimal GetVatPercentage(Entity quoteLine, IOrganizationService service)
        {
            Guid vatId = (Guid)quoteLine["capdcx_valueaddedtax"];
            ColumnSet attributesVatEntity = new ColumnSet(new string[]
            {
                "capdcx_valueaddedtaxpercentage"
            });
            Entity vatEntity = service.Retrieve("capdcx_valueaddedtax", vatId, attributesVatEntity);
            //Entity vatEntity = service.Retrieve("capdcx_valueaddedtax", (Guid)quoteLine["capdcx_valueaddedtax"], new ColumnSet("capdcx_valueaddedtaxpercentage"));

            decimal vatPercentageAsWholeNumber = 0;
            if (vatEntity.Attributes.Contains("capdcx_valueaddedtaxpercentage"))
            {
                vatPercentageAsWholeNumber = (decimal)vatEntity.Attributes["capdcx_valueaddedtaxpercentage"];
            }
            
            decimal vatPercentage = vatPercentageAsWholeNumber / 100;
            return vatPercentage;
        }

Thanks a lot!

Regards,
malliendk

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文