web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

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    }}
 
I have the same question (0)
  • Community member Profile Picture
    5 on at
    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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 108 Super User 2025 Season 2

#2
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans