Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Unable to cast object of type

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

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

  • Verified answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Unable to cast object of type

    Hello,

    Also replace line

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

    with

    createinvline["isproductoverridden"] = true;

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Unable to cast object of type

    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

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Unable to cast object of type

    Hello,

    I believe your code fails at line

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

    Try to use

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

    instead

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

News and Announcements

Announcing Category Subscriptions!

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,359 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,382 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans