使用类型为optionset的字段创建新实体

时间:2012-05-01 21:23:51

标签: dynamics-crm-2011

我有一个html表单,它发布一个aspx页面的帖子,该页面使用SOAP Web服务连接到CRM。页面后面的代码在CRM中创建了一个实体。我在后面的代码中使用了IOrganizationService。

代码看起来像

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并将其设置为实体时,我收到错误。关于如何使用选项集创建实体的任何想法?

由于

1 个答案:

答案 0 :(得分:3)

取决于您获得的错误,但如果引导类型为Microsoft.Xrm.Sdk.Entity,则可能需要替换现有值或添加新值。

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

重读我注意到你已经(大概)将错误放在评论中。在这种情况下,我建议问题是您需要指定类型OptionSetValue

的值