2017-02-13 19 views
0

我想創建一個地址記錄,但我不斷收到錯誤。MS CRM:創建地址記錄不正確的屬性值類型錯誤

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指的是聯繫人,所以我沒有看到問題是什麼。

+0

錯誤實際上是拋出行'Guid id = service.Create(newAddress);'。該錯誤永遠不會拋出在Entity.Attributes集合中添加/修改項目,因爲Entity類沒有類型意識。如果您使用早期綁定類型(來自'CrmSvcUtil.exe'),將會給你類型警告。 – Nicknow

回答

1

你需要改變這一行:

newAddress.Attributes["addresstypecode"] = addressTypeCode;

要:

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

因爲它是一個選項設置的類型必須爲OptionSetValue,不int

+0

謝謝,我應該看到那一個 –