MS CRM:创建地址记录引发不正确的属性值类型错误

MS CRM: Create Address Record Throws Incorrect Attribute Value Type Error

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

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

在设置对象类型代码的行上抛出错误。我知道错误代码的意思是 "Invalid argument"。解决方案2中提到了联系人,所以我看不出问题是什么。

您需要更改此行:

newAddress.Attributes["addresstypecode"] = addressTypeCode;

收件人:

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

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