Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Answered

Plugin: Input string not in the correct format

Posted on by 1,795

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;
                }
            }
        }
    }
}

  • JDMCRM Profile Picture
    JDMCRM 1,795 on at
    RE: Plugin: Input string not in the correct format

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

  • Verified answer
    a33ik Profile Picture
    a33ik 84,321 Most Valuable Professional on at
    RE: Plugin: Input string not in the correct format

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

  • JDMCRM Profile Picture
    JDMCRM 1,795 on at
    RE: Plugin: Input string not in the correct format

    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.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,321 Most Valuable Professional on at
    RE: Plugin: Input string not in the correct format

    Yeah, my fault. Change it to

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

  • JDMCRM Profile Picture
    JDMCRM 1,795 on at
    RE: Plugin: Input string not in the correct format

    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"];

  • Verified answer
    a33ik Profile Picture
    a33ik 84,321 Most Valuable Professional on at
    RE: Plugin: Input string not in the correct format

    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);

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,532 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,501 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans