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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Unable to cast object of type

(0) ShareShare
ReportReport
Posted on by

Hi,

I'm new to Plugins and C# I'm trying to create a simple plugin which will create a line item for invoice based on "freightamount" field values, However, I'm stuck with the below error.

This is a demo Plugin Start from Ankur : Create
IsPriceOverwriddenMicrosoft.Xrm.Sdk.Money
end IsPriceOverwridden
priceperunit
ExceptionSystem.InvalidCastException: Unable to cast object of type 'Microsoft.Xrm.Sdk.Money' to type 'System.IConvertible'.
at System.Convert.ToDecimal(Object value)
at PluginAnkur.AnkurFirstPlugin.Execute(IServiceProvider serviceProvider)
This is a demo Plugin end from Ankur

Here is how my code looks like:

using System;
using Microsoft.Xrm.Sdk;


namespace PluginAnkur
{
public class AnkurFirstPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{

ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService organizationService = servicefactory.CreateOrganizationService(context.UserId);

tracing.Trace("This is a demo Plugin Start from Ankur : " + context.MessageName);

if (context.InputParameters.Contains("Target"))
{
if(context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "invoice")
try
{
//Entity inv = new Entity("invoice");
if (entity["freightamount"] != null)
{
Entity createinvline = new Entity("invoicedetail");
createinvline["productdescription"] = "freightamount";
tracing.Trace("IsPriceOverwridden"+ entity["freightamount"].ToString());
createinvline["isproductoverridden"] = new OptionSetValue(1);
tracing.Trace(" end IsPriceOverwridden");
tracing.Trace(" priceperunit");
createinvline["priceperunit"] = new Money(Convert.ToDecimal(entity["freightamount"]));
tracing.Trace("end priceperunit");
tracing.Trace(" quantity");
createinvline["quantity"] = Convert.ToDecimal("1");
tracing.Trace("end quantity");

if (context.OutputParameters.Contains("id"))
{
createinvline["invoiceid"] = new EntityReference(entity.LogicalName, new Guid(context.OutputParameters["id"].ToString()));
}

organizationService.Create(createinvline);
}
}
catch(Exception e)
{ tracing.Trace("Exception" + e.ToString()); }
}
}
tracing.Trace("This is a demo Plugin end from Ankur");
}

}

}

A help will be much appreciated.

Thanks in advance.

*This post is locked for comments

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

    Hello,

    I believe your code fails at line

    createinvline["priceperunit"] = new Money(Convert.ToDecimal(entity["freightamount"]));

    Try to use

    createinvline["priceperunit"] = entity["freightamount"]);

    instead

  • Community Member Profile Picture
    on at

    You are right about the line where the code fails. However, when I try to use your suggestion I'm getting error on the same line with

    Message Block This is a demo Plugin Start from Ankur : Create

    IsPriceOverwriddenMicrosoft.Xrm.Sdk.Money

    end IsPriceOverwridden

    priceperunit

    end priceperunit

    quantity

    end quantity

    ExceptionSystem.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Type Mismatch: Type of Attribute: InvoiceDetail.isproductoverridden is: System.Boolean. However, Type of passed-in value is: Microsoft.Xrm.Sdk.OptionSetValue (Fault Detail is equal to Exception details:

    ErrorCode: 0x80040203

    Message: Type Mismatch: Type of Attribute: InvoiceDetail.isproductoverridden is: System.Boolean. However, Type of passed-in value is: Microsoft.Xrm.Sdk.OptionSetValue

    TimeStamp: 2018-10-05T15:20:05.0395462Z

    OriginalException: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Type Mismatch: Type of Attribute: InvoiceDetail.isproductoverridden is: System.Boolean. However, Type of passed-in value is: Microsoft.Xrm.Sdk.OptionSetValue (Fault Detail is equal to Exception details:

    ErrorCode: 0x80040203

    Message: Type Mismatch: Type of Attribute: InvoiceDetail.isproductoverridden is: System.Boolean. However, Type of passed-in value is: Microsoft.Xrm.Sdk.OptionSetValue

    TimeStamp: 2018-10-05T15:20:05.0395462Z

    --

    Exception details:

    ErrorCode: 0x80040203

    Message: Type Mismatch: Type of Attribute: InvoiceDetail.isproductoverridden is: System.Boolean. However, Type of passed-in value is: Microsoft.Xrm.Sdk.OptionSetValue

    TimeStamp: 201...).

    This is a demo Plugin end from Ankur Message Block-Trace text from the plug-in.

    This is a demo Plugin Start from Ankur : Create

    IsPriceOverwriddenMicrosoft.Xrm.Sdk.Money

    end IsPriceOverwridden

    priceperunit

    end priceperunit

    quantity

    end quantity

    ExceptionSystem.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Type Mismatch: Type of Attribute: InvoiceDetail.isproductoverridden is: System.Boolean. However, Type of passed-in value is: Microsoft.Xrm.Sdk.OptionSetValue (Fault Detail is equal to Exception details:

    ErrorCode: 0x80040203

    Message: Type Mismatch: Type of Attribute: InvoiceDetail.isproductoverridden is: System.Boolean. However, Type of passed-in value is: Microsoft.Xrm.Sdk.OptionSetValue

    TimeStamp: 2018-10-05T15:20:05.0395462Z

    OriginalException: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Type Mismatch: Type of Attribute: InvoiceDetail.isproductoverridden is: System.Boolean. However, Type of passed-in value is: Microsoft.Xrm.Sdk.OptionSetValue (Fault Detail is equal to Exception details:

    ErrorCode: 0x80040203

    Message: Type Mismatch: Type of Attribute: InvoiceDetail.isproductoverridden is: System.Boolean. However, Type of passed-in value is: Microsoft.Xrm.Sdk.OptionSetValue

    TimeStamp: 2018-10-05T15:20:05.0395462Z

    --

    Exception details:

    ErrorCode: 0x80040203

    Message: Type Mismatch: Type of Attribute: InvoiceDetail.isproductoverridden is: System.Boolean. However, Type of passed-in value is: Microsoft.Xrm.Sdk.OptionSetValue

    TimeStamp: 201...).

    This is a demo Plugin end from Ankur

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

    Hello,

    Also replace line

    createinvline["isproductoverridden"] = new OptionSetValue(1);

    with

    createinvline["isproductoverridden"] = true;

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans