Announcements
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
Hello,
Also replace line
createinvline["isproductoverridden"] = new OptionSetValue(1);
with
createinvline["isproductoverridden"] = true;
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
Hello,
I believe your code fails at line
createinvline["priceperunit"] = new Money(Convert.ToDecimal(entity["freightamount"]));
Try to use
createinvline["priceperunit"] = entity["freightamount"]);
instead
André Arnaud de Cal... 291,359 Super User 2024 Season 2
Martin Dráb 230,382 Most Valuable Professional
nmaenpaa 101,156