MS CRM:创建地址记录会导致错误的属性值类型错误

时间:2017-02-13 23:20:56

标签: c# dynamics-crm-2011

我正在尝试创建一个地址记录,但我一直收到错误。

The application terminated with an error.
Code: -2147220989
Message: Incorrect attribute value type System.Int32

代码如下

public const int OBJECT_TYPE_CONTACT = 2;



int addressTypeCode = 3; //Primary Address
if (i == 2) addressTypeCode = 1; //Billing Address
if (i == 3) addressTypeCode = 2; //Shipping Address

Entity newAddress = new Entity("customeraddress");
newAddress.Attributes["parentid"] = new EntityReference("contact", existingContact.Id);
newAddress.Attributes["addresstypecode"] = addressTypeCode;
newAddress.Attributes["objecttypecode"] = OBJECT_TYPE_CONTACT;
newAddress.Attributes["line1"] = "temp1";
newAddress.Attributes["line2"] = "temp2";
newAddress.Attributes["line3"] = "temp3";
newAddress.Attributes["city"] = "temp3";
newAddress.Attributes["stateorprovince"] = "temp3";
newAddress.Attributes["postalcode"] = "temp3";
newAddress.Attributes["country"] = "temp3";

Guid id = service.Create(newAddress);

错误抛出了设置objecttypecode的行。我知道错误代码意味着“无效的参数”。在解决方案中2指的是联系人,所以我看不出问题所在。

1 个答案:

答案 0 :(得分:1)

您需要更改此行:

newAddress.Attributes["addresstypecode"] = addressTypeCode;

要:

newAddress.Attributes["addresstypecode"] = new OptionSetValue(addressTypeCode);

因为它是一个选项集,所以类型必须是OptionSetValue,而不是int