web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Plugin: Input string not in the correct format

(0) ShareShare
ReportReport
Posted on by 1,799

Hi, 

The following plugin, firing on create of the invoice line entity, fails with the error message: <Message>An unexpected error occurred from ISV code. (ErrorType = ClientError) Unexpected exception from plug-in (Execute): InvoiceLineCalculation.InvoiceLineCalculation: System.Exception: Input string was not in a correct format.</Message>. Do you know what might cause this issue?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;

namespace InvoiceLineCalculation
{
   public class InvoiceLineCalculation : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the tracing service
            ITracingService tracingService =
            (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            // Obtain the execution context from the service provider.  
            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));

            // The InputParameters collection contains all the data passed in the message request.  
            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parameters.  
                Entity entity = (Entity)context.InputParameters["Target"];

                // Obtain the organization service reference which you will need for  
                // web service calls.  
                IOrganizationServiceFactory serviceFactory =
                    (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                try
                {
                    if (entity.Attributes.Contains("priceperunit")
                    && entity.Attributes.Contains("volumediscountamount")
                    && entity.Attributes.Contains("quantity")
                    && entity.Attributes.Contains("manualdiscountamount")
                    && entity.Attributes.Contains("tax"))
                    {
                        decimal pricePerUnit = Convert.ToDecimal(entity["priceperunit"].ToString());
                        decimal volumeDiscount = Convert.ToDecimal(entity["volumediscountamount"].ToString());
                        decimal quantity = Convert.ToDecimal(entity["quantity"].ToString());
                        decimal discountAmount = Convert.ToDecimal(entity["manualdiscountamount"].ToString());
                        decimal tax = Convert.ToDecimal(entity["tax"].ToString());

                        decimal calculation = (((pricePerUnit - volumeDiscount) * quantity) - discountAmount   tax);
                        entity["extendedamount"] = calculation;
                    }
                }
                catch (FaultException ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in InvoiceLineCalculation Plugin.", ex);
                }

                catch (Exception ex)
                {
                    tracingService.Trace("InvoiceLineCalculation: {0}", ex.ToString());
                    throw;
                }
            }
        }
    }
}

I have the same question (0)
  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    The main reason why you get an error is related to the way you're getting the value from fields. For example priceperunit field is not Decimal - it's money (as well as volumediscountamount, manualdiscountamount, tax) and in order to fix your code you can use the following code:

    decimal pricePerUnit = entity.GetAttributeValue<Money>("priceperunit").Value;

    decimal volumeDiscount = entity.GetAttributeValue<Money>("volumediscountamount").Value;

    decimal quantity = entity.GetAttributeValue<decimal>["quantity"];

    decimal discountAmount = entity.GetAttributeValue<Money>("manualdiscountamount").Value;

    decimal tax = entity.GetAttributeValue<Money>("tax").Value;

    decimal calculation = (((pricePerUnit - volumeDiscount) * quantity) - discountAmount + tax);

    entity["extendedamount"] = new Money(calculation);

  • JDMCRM Profile Picture
    1,799 on at

    Thanks Andrew, that's good to know.

    Unfortuantely I can't build my project as I get the error message: CS0021 Cannot apply indexing with [] to an expression of type 'method group'

    This is for line:

    decimal quantity = entity.GetAttributeValue<decimal>["quantity"];

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Yeah, my fault. Change it to

    decimal quantity = entity.GetAttributeValue<decimal>("quantity");

  • JDMCRM Profile Picture
    1,799 on at

    Thanks Andrew, that did the job.

    For some reason the extendedamount field isn't updating even though the plugin appears to fire and there are no errors in the plug-in trace log. I'll do some further debugging, but thank you for getting me to this point.

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Quick check - to make it work your plugin should work in pre-validate or pre-operate mode. Is it so?

  • JDMCRM Profile Picture
    1,799 on at

    That's resolved it. Thank you very much! Saved me many hours today.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 81 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans