Hank Knah
Hank Knah

Reputation: 101

Convert from TSQL money to decimal in CRM plugin?

Here is the point I'm at. new_TotalAssets is defined in tsql as Money, as Dynamics CRM defined it. I need to convert it to decimal so I can add another value to it and update the value back in tsql. When I tried to do that with all money fields, I got the error: Operand + cannot be applied to operands of type 'Microsoft.Xrm.Sdk.Money' and 'Microsoft.Xrm.Sdk.Money'. What is the correct way to make this work?

Decimal TotA = decimal.Parse("0.0000");
Entity opp = service.Retrieve("opportunity", Guid.Parse(oppid), new ColumnSet(new string[] { "new_totalassets" })); // no runtime error here

//TotA = (Decimal)opp.Attributes["new_totalassets"];                        // specified cast is not valid
//TotA = Convert.ToDecimal(opp.Attributes["new_totalassets"]);              // Unable to cast object of type 'Microsoft.Xrm.Sdk.Money' to type 'System.IConvertible'.
//TotA = Convert.ToDecimal(opp.Attributes["new_totalassets"].ToString());   // input string was not in a correct format
//TotA = Decimal.Parse(opp.Attributes["new_totalassets"].ToString());       // input string was not in a correct format

Upvotes: 0

Views: 4022

Answers (2)

jhoung
jhoung

Reputation: 1

Convert.ToDecimal(opp.GetAttributeValue<Money>(item).Value);

Upvotes: 0

Renjith
Renjith

Reputation: 765

Before summing up the data please try to use .Value of the attribute. Something like this,

decimal totalValue=((Money)opp.Attributes["new_totalassets"]).Value;

Now use the totalValue for the summing up or any other operations as required.

Hope this helps!!!

Upvotes: 2

Related Questions