Skip to main content

Problems with picklists

editSubscribe (1) ShareShare
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    }}