Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Problems with picklists

(1) ShareShare
ReportReport
Posted on by 5
I'm having a strange problem with a plugin I am developing. We are adding picklist support to the plugin. The purpose is to cleanse data, and correct user inputs if the plugin deems some part of the input as incorrect.

When cleansing picklist inputs I've run into a strange problem. In all cases, when a user creates a new record, the picklist value is set to /--/, or the empty /null/ value. All of my internal logging shows that this is not correct, and a value in the picklist should have been selected.

What is interesting is that no matter what the value is going into the cleanse process, on a /create/, the value is set to /null/. On /update/ the value is correctly cleansed.

Is there any thing in the way that dynamics processes picklists that could help me diagnose this issue? This snippet of code always works with updating existing records, but fails only when creating new records. From my understanding, there shouldn't be a difference between those use cases.
 
//entity is of type Microsoft.Xrm.sdk.Entity//CrmFieldName is of type String// This function queries the dataverse table to determine if the CrmFieldName we are testing is a picklist// Is there a better way to do this? I'd rather not query for each and every field if possible// OR is it possible that Dynamics is optimized so that this is not a performance concern?PicklistAttributeMetadata picklist = queryForPicklistMetadata(entity, CrmFieldName);if (picklist != null){    tracingService?.Trace(/Picklist detected/);    bool matchFound = false;    foreach (OptionMetadata option in picklist.OptionSet.Options)    {        // If the label of the integer representation of the value matches what we think the value should be        if (option.Label.UserLocalizedLabel.Label == sFieldValue)        {            tracingService?.Trace(/Matching Picklist Option found : /+ option.Value.ToString());            sFieldValue = option.Value.ToString();            matchFound = true;            // We return the first match found            // Dynamics should make it impossible for there to be multiple matches,            // but, just in case, we are defaulting to this behavior.            // Changing the optionset value must be done with an int, it cannot cast a string to an int.            // This is the line that seems to cause the problem is defaulting to null in the picklist            // However, it is also the same line that updates the picklist            // I've confirmed we are not passing a bad value to SetOrAddAttribute            // Is there something about the way this works that I am not taking into account?            entity.SetOrAddAttribute(CrmFieldName, Int32.Parse(sFieldValue));            break;        }    }    if (!matchFound)    {        // Error Handling    }}
 
  • Community member Profile Picture
    5 on at
    Problems with picklists
    I'm not sure why my code is being formatted like that, so here it is posted directly into the box.
     
     
    //entity is of type Microsoft.Xrm.sdk.Entity
    //CrmFieldName is of type String
     
    // This function queries the dataverse table to determine if the CrmFieldName we are testing is a picklist
    // Is there a better way to do this? I'd rather not query for each and every field if possible
    // OR is it possible that Dynamics is optimized so that this is not a performance concern?
    PicklistAttributeMetadata picklist = queryForPicklistMetadata(entity, CrmFieldName);
    if (picklist != null)
    {
        tracingService?.Trace("Picklist detected");
        bool matchFound = false;
        foreach (OptionMetadata option in picklist.OptionSet.Options)
        {
            // If the label of the integer representation of the value matches what we think the value should be
            if (option.Label.UserLocalizedLabel.Label == sFieldValue)
            {
                tracingService?.Trace("Matching Picklist Option found : "+ option.Value.ToString());
                sFieldValue = option.Value.ToString();
                matchFound = true;
     
                // We return the first match found
                // Dynamics should make it impossible for there to be multiple matches,
                // but, just in case, we are defaulting to this behavior.
     
                // Changing the optionset value must be done with an int, it cannot cast a string to an int.
                // This is the line that seems to cause the problem is defaulting to null in the picklist
                // However, it is also the same line that updates the picklist
                // I've confirmed we are not passing a bad value to SetOrAddAttribute
                // Is there something about the way this works that I am not taking into account?
                entity.SetOrAddAttribute(CrmFieldName, Int32.Parse(sFieldValue));
                break;
            }
        }
        if (!matchFound)
        {
            // Error Handling
        }
    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 671 Super User 2025 Season 1

#2
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 167 Super User 2025 Season 1

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 138 Most Valuable Professional

Product updates

Dynamics 365 release plans