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