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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans