2012-05-01 27 views
2

我有一個HTML表單,它向使用SOAP Web服務連接到CRM的aspx頁面發佈信息。該頁面後面的代碼在CRM中創建一個實體。我在後面的代碼中使用IOrganizationService。創建一個字段爲optionset的新實體

代碼看起來像

IOrganizationService service = (IOrganizationService)serviceProxy;      
Entity lead = new Entity("lead"); 
string fieldValue = string.Empty; 

foreach (string key in Request.Form.AllKeys) 
{ 
    if (key.Equals(SubmitKey, StringComparison.InvariantCultureIgnoreCase) == false && 
     key.Equals(CRMHostKey, StringComparison.InvariantCultureIgnoreCase) == false && 
     key.Equals(redirectErrorURLKey, StringComparison.InvariantCultureIgnoreCase) == false && 
     key.Equals(redirectSuccessURLKey, StringComparison.InvariantCultureIgnoreCase) == false) 
    { 
     if (!string.IsNullOrEmpty(Request.Form[key])) 
     { 
      fieldValue = Request.Form[key].Trim(); 
     } 
     else 
     { 
      fieldValue = string.Empty; 
     } 

     if (key.Equals("new_contacttypechoices", StringComparison.InvariantCultureIgnoreCase)) 
     { 
      lead[key] = new KeyValuePair<string, int>("Email", 100000000); 
      //OptionMetadata objOM = GetOptionMetadata("lead", "new_contacttypechoices", fieldValue, service); 
      //lead[key] = objOM; 
      //lead[key] = 100000000; //Incorrect attribute value type System.Int32 
      //lead[key] = fieldValue; //Incorrect attribute value type System.String 
     } 
     else 
     { 
      lead[key] = fieldValue; 
     } 
    } 
    newLeadID = service.Create(lead); 
} 

截圖領域的enter image description here

我得到一個錯誤,當我嘗試

lead[key] = fieldValue 

我得到一個錯誤,當我嘗試

lead[key] = 100000000 

我得到一個錯誤,當我嘗試

lead[key] = new KeyValuePair<string, int>("Email", 100000000); 

我得到一個錯誤,當我得到了OptionMetaData並將其設置爲實體。有關如何使用選項集創建實體的任何想法?

感謝

+0

你會得到什麼錯誤? – glosrob

+0

糟糕 - 我的apols,剛剛注意到內嵌評論 – glosrob

回答

3

取決於你所得到的錯誤,但如果鉛是Microsoft.Xrm.Sdk.Entity型的,它可能是你需要或者替換現有值或添加一個新的。

if (lead.Attributes.Contains(key)) 
{ 
    lead[key] = new OptionSetValue(100000000);   
} 
else 
{ 
    lead.Attributes.Add(key, new OptionSetValue(100000000));   
} 

重讀我注意到你已經把(可能是)錯誤放在註釋中。在這種情況下,我建議的問題是,你需要指定一個值類型OptionSetValue

相關問題