CRM 2011: Data Types conversion problem
-
29. května 2012 15:23
Hi all, Currently I'm facing a problem in data types conversion in my plugin, I need to calculate some money fields so I hav used the below code sample:
Decimal financingFees = (netToSupplier[j] * (Decimal)PRODUCT_FINANCING) / 100; QuoteProdcutUpdate["intercom_financingfees"] = financingFees;the netToSupplier is Deciaml while the PRODUCT_FINANCING is float and I need to update a currency field
Please advise..
Všechny reakce
-
29. května 2012 15:47Moderátor
From point of conversion everything looks good.
I believe that source of your error is other. You've wrote that you calculate some money. Is intercom_financingfees Money type? In case you should use following code:
Decimal financingFees = (netToSupplier[j] * (Decimal)PRODUCT_FINANCING) / 100; QuoteProdcutUpdate["intercom_financingfees"] = new Money(financingFees);
Also when you work with Money field you should always fill transactioncurrencyid field with reference to currency record - type EntityReference.Microsoft CRM Freelancer
My blog (english)
Мой блог (русскоязычный)
- Navržen jako odpověď Andrii ButenkoMVP, Moderator 2. června 2012 5:03
- Označen jako odpověď mo32 6. června 2012 8:58
-
29. května 2012 16:28
And why should I divide the result by 100 ?
Also why should I re-fill the transactioncurrencyid field ? Is to calculate the base field ? and what if I don't need to update the record currency ?
Thanks and best regards..
-
29. května 2012 16:46Moderátor
Regarding dividing by 100 - I have no clue. This is the formula you have provided. I believe that netToSupplier[j] contains some kind of percentage (from 0 to 100) and dividing by 100 will return percentage from froduct_financing value.
Regarding transactioncurrencyid - you should fill it during creation of record and during updates when transactioncurrencyid field was not filled before.
Microsoft CRM Freelancer
My blog (english)
Мой блог (русскоязычный)
- Navržen jako odpověď Andrii ButenkoMVP, Moderator 2. června 2012 5:03
-
29. května 2012 20:51
I think the problem is with the casting itself, as I have tried the following:
Decimal financingFees = (netToSupplier[j] * (Decimal)PRODUCT_FINANCING) / 100; QuoteProdcutUpdate["intercom_financingfees"] = new Money( financingFees ); throw new InvalidPluginExecutionException("DONE");But when tried to execute the plugin, it throws a Specified cast is not valid exception
Thanks and best regards..
- Upravený mo32 29. května 2012 20:52
- Navržen jako odpověď Andrii ButenkoMVP, Moderator 2. června 2012 5:03
-
29. května 2012 21:04Moderátor
Could you please provide code which is located above this? Where you retrieve netToSupplier and PRODUCT_FINANCING values?Microsoft CRM Freelancer
My blog (english)
Мой блог (русскоязычный)
- Navržen jako odpověď Andrii ButenkoMVP, Moderator 2. června 2012 5:03
-
29. května 2012 21:23
double PRODUCT_FINANCING = 0; Entity preMessageImage; if (context.PreEntityImages.Contains("PreImage") && context.PreEntityImages["PreImage"] is Entity) { preMessageImage = (Entity)context.PreEntityImages["PreImage"]; PRODUCT_FINANCING = (double)preMessageImage.Attributes["intercom_productfinancing"]; } Decimal[] netToSupplier = new Decimal[count]; Entity Entity1= new Entity(); for (int j = 0; j < quoteProductCount; j++) { Entity1 = (Entity)QuoteProductQueryRetrieve.Entities[j]; netToSupplier[j] = (Decimal)Entity1.Attributes["intercom_nettosupplier"]; }However you gave me the key to what could be the source of the problem, I will revise my code to retrieve the values...thanks :) -
29. května 2012 21:40Moderátor
I see here several possible sources of your Exception:
PRODUCT_FINANCING = (double)preMessageImage.Attributes["intercom_productfinancing"];
and
netToSupplier[j] = (Decimal)Entity1.Attributes["intercom_nettosupplier"];
You should recheck that type of field is valid for cast.
Small hint - you have no need to write Attributes everywhere. Entity["fieldname"] will work as well.
What I really can suggest you to get the result faster - you should debug your code using VS.
Microsoft CRM Freelancer
My blog (english)
Мой блог (русскоязычный)
- Navržen jako odpověď Andrii ButenkoMVP, Moderator 2. června 2012 5:03
- Označen jako odpověď mo32 6. června 2012 8:58