2
votes

I have a problem in my CRM 2011 plugin.

var QuoteProduct = crm.QuoteDetailSet.Where(c 
  => c.QuoteDetailId == QPID).First();
double Tax = (double)(
  (QuoteProduct.BaseAmount 
    - QuoteProduct.ManualDiscountAmount.GetValueOrDefault() 
    - QuoteProduct.VolumeDiscountAmount.GetValueOrDefault()) / 20);
QuoteProduct.Attributes["tax"] = Tax;
crm.UpdateObject(QuoteProduct);
crm.SaveChanges();

The error occurs on the save changes line. The error details are as follows.

Microsoft.Xrm.Sdk.SaveChangesException was unhandled by user code
Message=An error occured while processing this request.
Source=Microsoft.Xrm.Sdk
StackTrace:
at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.SaveChanges(SaveChangesOptions options)
at Plugin.TaxCreator(IOrganizationService service, Guid QPID) in C:\Users\mycrm\Desktop\BMSD.QuoteProduct.Tax\BMSD.QuoteProduct.Tax\BMSD.QuoteProduct.Tax.cs:line 62
at Plugin.Execute(IServiceProvider serviceProvider) in C:\Users\mycrm\Desktop\BMSD.QuoteProduct.Tax\BMSD.QuoteProduct.Tax\BMSD.QuoteProduct.Tax.cs:line 38
at Microsoft.Crm.Extensibility.V5PluginProxyStep.ExecuteInternal(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext context)
InnerException: System.ServiceModel.FaultException Message=System.InvalidCastException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #BE061894
Source=Microsoft.Crm.Extensibility
StackTrace:
at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Execute(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
at Microsoft.Crm.Extensibility.InprocessServiceProxy.ExecuteCore(OrganizationRequest request)
at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.Execute(OrganizationRequest request)
at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.SaveChange(OrganizationRequest request, IList`1 results)
InnerException:

2

2 Answers

4
votes

From your stack trace there is a System.InvalidCastException

It means value of some attribute has incorrect type. Since you are changing only 'tax' attribute, then its type is incorrect. Most probably "tax" is a Money field, so I guess you should assign variable of type decimal to it, not double. Try something like this:

decimal Tax = (decimal)((QuoteProduct.BaseAmount 
  - QuoteProduct.ManualDiscountAmount.GetValueOrDefault() 
  - QuoteProduct.VolumeDiscountAmount.GetValueOrDefault()) / 20);
1
votes

Based on the information presented, it's probably conversion problem when you cast to double. The problem is that CRM by default presents reals with a higher precision than what you try to box it into.

  • double is 8 bytes tall and holds significance of 15 digits
  • decimal is 12 bytes tall and holds significance of 28 digits

So, even though the actual value you're handling would fit in any of them (and double is de facto standard for non-integers in C#), the computer reacts and gives you a warning that you're putting something (potentially) really big into a (relatively) small hole.

You need to redeclare the tax as follows.

//double tax;
decimal tax = QuoteProduct.BaseAmount 
  - QuoteProduct.ManualDiscountAmount.GetValueOrDefault() 
  - QuoteProduct.VolumeDiscountAmount.GetValueOrDefault();
QuoteProduct.Attributes["tax"] = tax / 20;

Or you could manually convert the decimal values to double using the built-in converting classes. However, given that you're putting the result into a field (that surely manages decimal type), it's an unnecessary detour in my opinion.

You can read more on the different type by clicking here.