1
votes

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?

4

4 Answers

2
votes

Setting Currency

Get : -

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

Post : -

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

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).

0
votes

The transaction currencyid type is a 'lookup' according to the contact entity metadata on http://msdn.microsoft.com/en-us/library/gg328530.aspx . Because it is a lookup type you need to pass the value differently. When you create the record via the webservice you you have to build an EntityReference for the currency to pass to the attributes of the contact entity before you call the .create command on the CRM webservice. If you try and pass it as a string it won't do anything no errors and won't accept it like you described. I'm still new to the CRM webservice but give this a try and see if it works.

It will probably be something like this: (metadata for reference http://msdn.microsoft.com/en-us/library/gg327883.aspx)

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

contactGoingToCrm.Attributes.Add(new keyvaluepair<string,object>("currencyid",currencyType));
-2
votes

The C# code may look like this.

myEntity.Attributes["abc_transactionamount"] = new Money((decimal)obj.TotalAmountToPay);