Bhavya Kesaria
Bhavya Kesaria

Reputation: 25

How to convert string into optionset and insert it into dynamic CRM option set

I wanted to convert a field from string to Option set AND insert it into in dynamic CRM entity. So please help me if anyone knows how to do that..

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

Upvotes: 0

Views: 4333

Answers (2)

Charan Raju C R
Charan Raju C R

Reputation: 768

tsukumogami's answer also correct. But, to retrieve it dynamically, you can use the below code.

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

Upvotes: 2

duongntbk
duongntbk

Reputation: 670

Base on the code you provided, I guess you have a DropDownList or something similar which provide a list of options and now you want to set an OptionSet field of your entity bases on what users chose. If that's not what you want, please clarify further.

Let say in CRM you have an OptionSet with the following options:

Name    Value
Apple   100000000
Orange  100000001
Lemon   100000002

Now in your list, you have the following choices:

Apple   
Orange   
Lemon    

The first thing you do is to map user choice with the value in CRM, in other word, if your user choose Orange, you have to match it to 100000001, if they choose Lemon, match it with 100000002... This can be done with switch case, a dictionary, or if-else...

After you have the value, add it to your entity like this:

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

In your case, you can write it like this:

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)

Upvotes: 2

Related Questions