Combo box doesn't respect default value when no item is selected

Combo box doesn't respect default value when no item is selected

  
Hello developers

I have a weird situation with one combo box. I have a local variable assigned to it, and the default value of this variable is -1. Also, the combo box has one value within the "special list property" that reads "choose product" and the value is -1. In the on change event of this combo box, I can observe with the debugger that when I change the value of my combo box, it's behavior is ok, and the variable assigned to the combo box is updated without problem. Now... if I choose the first value of the combo box (the first value is "choose product", with -1 value of the special list property), the variable assigned to the combo box is 0 instead of -1. This is a big issue, because 0 value already exists in the combo box and is the second item.  If I do an HTML inspection of the page, I find that the first value of the combo box is "__ossli_-1", not even -1, and when it is chosen, the variable assigned to the Combo Box is 0. 

Why this strange behavior? How can I solve it??? 

Thank you!
Hi Samuel,

First, your question is not a usability question, so you should post questions like these in the Tech forum, where it'll get much more attention.

Secondly, I think you misunderstand the way combo boxes work with their Variable vs. their Special List. If you do not have a special list, it's straight forward: the combo box is populated with items from the list, so the Variable contains whatever is the first item's Identifier Value on start-up, unless the Variable has another value than its type's null value (NullIdentifier(), NullTextIdentifier(), 0, 0.0, "" or False depending on its type) and that value is in the list, in which case the default selected item is set to that.

If you add a Special List to the mix, you need to understand that whatever value from the Special List you select, your Variable is going to be set to its null value. That's why your variable is 0. This is good, because it signals your application that an item from the Special List is selected, as opposed to an item from the Source Record List. Keep in mind that most of the time, you'll populate a combo box with the output from a query, the Entity's Id being the Source Identifier Attribute, and your Variable being of type Entity Identifier. It would almost never make sense in such a scenario to want to assign the Value from the special list to the Variable.

But... of course, if your special list is more than a single item, you need to know which item has been selected. Enter the Special Variable. The Special Variable is a variable you specify to receive the Value of the selected Special List item. If the user selects an item from the Source Record List, it is set to its null value. Otherwise, it's filled with the Value of the selected Special List Item.

So, to wrap up, you didn't experience "strange behaviour", but quite normal, documented, behaviour of the Platform. To solve your problem, if you have a single item in the Special List, just test for 0 or NullIdentifier() (depending on your Variable's type) to see if the Special List Item is selected, if you have more than a single one, specify a Special Variable, and test its content when the Variable is null.
Thank you for your reply

I am not sure if I can solve the problem by merely test for 0 or NullIdentifier() because the thing is, that when the combo box is populated, I actually have a record with the value of 0 coming from the database. If I test for 0, I will get two results, the one from the special list, and the existing record too.

What should I do in this case?

Thank you
Solution
Hi Samuel,

If you have records with an Id of 0 coming from the database, you have a serious problem. The OutSystems Platform is not set up to deal with this. Not only in this specific case you'll have problem, but everywhere. You really must prevent Ids with a 0 value from entering the realm of the Platform.
Solution
Ok sir. Thank you for the information and your honesty.
Thank you for your explanation of the combo box behavior.  I was also trying to programmatically set it for new records in the Preparation step, but it was easier just to make sure the value I want as default just appears first in the list.

That's also an option, yeah. In general, if it doesn't make sense to sort the list alphabetically, putting the most used options at the top is most user friendly. The default being the first is the best choice.