如何将字符串转换为选项集并将其插入动态CRM选项集中



我想将字段从字符串转换为选项集,然后将其插入动态CRM实体中。所以请帮我如果有人知道该怎么做..

Entity accountEntity = new Entity("new_categoryoption");
accountEntity["new_categorylist"] = dt.Rows[i][1].ToString();

tsukumogami的答案也正确。但是,要动态检索它,您可以使用以下代码。

Entity accountEntity = new Entity("new_categoryoption");
accountEntity["new_categorylist"] = new OptionSet( GetOptionsSetValueOnText(service, "new_categoryoption", "new_categorylist", dt.Rows[i][1].ToString()));

public int GetOptionsSetValueOnText(IOrganizationService service, string entitySchemaName, string attributeSchemaName, string optionsetText)
{
    RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest
    {
        EntityLogicalName = entitySchemaName,
        LogicalName = attributeSchemaName,
        RetrieveAsIfPublished = true
    };
    RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
    PicklistAttributeMetadata retrievedPicklistAttributeMetadata = (PicklistAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;
    OptionMetadata[] optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
    int optionsetValue = 0;
    if (optionList.Length > 0)
    {
        optionsetValue = (from a in optionList
                    where a.Label.UserLocalizedLabel.Label == optionsetText
                    select a.Value).First();
    }
    return optionsetValue ;
}

基于您提供的代码的基础,我想您有一个下拉列表或类似的内容,这些列表提供了选项的列表,现在您想在用户选择的内容上设置实体基础的选项字段。如果那不是您想要的,请进一步澄清。

在CRM中说,您有一个选项,具有以下选项:

Name    Value
Apple   100000000
Orange  100000001
Lemon   100000002

现在在列表中,您有以下选择:

Apple   
Orange   
Lemon    

您要做的第一件事是用CRM中的值映射用户选择,换句话说,如果您的用户选择橙色,则必须将其匹配到100000001,如果他们选择柠檬,请与100000002匹配...可以使用开关案件,字典或if-else ...

来完成

拥有该值后,将其添加到您的实体中:

accountEntity["new_categorylist"] = new OptionSetValue(<value>)

在您的情况下,您可以这样写:

int optSetValue;
switch (dt.Rows[i][1].ToString())
{
    case "Apple":
        optSetValue = 100000000;
        break;
    case "Orange":
        optSetValue = 100000001;
        break;
    case "Lemon":
        optSetValue = 100000002;
        break;
    default:
        throw new Exception("Invalid choice");
}
accountEntity["new_categorylist"] = new OptionSetValue(optSetValue)

最新更新