Dynamics CRM 2011, setting currency field programmatically

20,635

Solution 1

Setting Currency

Get : -

var totalValue = ((Money)item.Attributes[attributeName]).Value;

Post : -

newSalesOrder[attributeName] = new Money((decimal)totalValue);

Solution 2

Because of multi-currency support, you have to Explicitly set the currency for that particular record while creating using SDK.

EntityReference currencyType = new EntityReference();
currencyType.Id = “(The Guid Of The Currency Type Goes Here)”;
currencyType.LogicalName = “transactioncurrency”;

entity.Attributes.Add(“transactioncurrencyid”,currencyType);

From CRM UI while creating, the lookup field transactioncurrencyid will be populated from user settings.

For the old records which got created already (when user settings was not set with default currency or in your case), you have to add that lookup into the form from Form editor, customize & publish. Then assign the currency in lookup for those records (may be bulk edit).

Share:
20,635
DeveloperM
Author by

DeveloperM

Updated on July 06, 2022

Comments

  • DeveloperM
    DeveloperM almost 2 years

    If I create a new Contact record manually in CRM 2011, the currency fields get created properly, the "$" is visible, and I can populate those fields and save the record.

    If I instantiate an IOrganizationService and create a Contact record programmatically, everything works except the currency fields. No error is generated that I can see; the record gets created, all other fields are populated but the currency fields are left blank.

    If I try to update those currency fields manually after creating the record programmatically, I get this error: A currency is required if a value exists in a money field. Select a currency and try again.

    My user record is set with a currency = US Dollar.

    Why is this working in CRM but not in C#? What do I need to do to get it to work?

  • Chirag
    Chirag over 9 years
    you can't convert a money field to decimal.