I have a canvas App. It contains a Data Card with Dropdown inside. It is bound to an SQL table ‘MyTable’; column ‘MyColumn’. The Dropdown has Default = Parent.Default. The parent (Data Card) has the following:
AllowedValues = DataSourceInfo('MyTable’, DataSourceInfo.AllowedValues, "MyColumn’")
Default = ThisItem.DataSourceKey
I want to improve the Default property of the Data Card this way:
when the Form is being opened in FormMode.New mode, the Default should always be the first item of the Allowed Values. As I understand, I should change the Data Card's Default property this way:
If(Form1.Mode = FormMode.New,
Could you please help me with the <???????????> part; how exactly would it look like? How do I get the first item of the Allowed Values list here?
Select your dropdown
Go to its advance setting
search "AllowEmptyValue" and set it as false
and use the below code
If(Form1.Mode = FormMode.New, "", ThisItem.DataSourceKey)
If this doesnt work try
First('MyTable’, DataSourceInfo.AllowedValues, "MyColumn’")
You can also try "coalesce" funct instead of first
Give these a try
My suggestion to you is to not change the Default property of the datacard. The default property and the update property play an integral role in the Unsaved property of the form. So, it should be left as ThisItem.DataSourceKey
Also, dependency on the Form Mode can be misleading. For example, you can have a new form without the form being in new mode, but instead in Edit.
So, the best thing to focus on is the Primary Key column (PK) of your underlying record. That will ALWAYS give you indication of the mode of the form.
If the PK column is Blank, then you are creating (new) a record. If it is NOT blank, then you are editing a record.
SO...all that said, instead look to your DefaultSelectedItems property (assuming you have a combobox in your datacard) and set it to:
If(IsBlank(ThisItem.<yourPKcolumnName>), First(Parent.AllowedValues), Parent.Default )
This will perform two important things:
1) If the record is new, then it will set the first item to be selected.
2) If the record is NOT new, then it will show the prior selections in the record you're editing.
I hope this is helpful for you.
My suggestion to you is to not change the Default property of the datacard.
SO...all that said, instead look to your DefaultSelectedItems property (assuming you have a combobox in your datacard) ....
So in addition to focusing on primary key, you mean it is better to make these kinds of changes to the property of the DataCardValueXX control that is inside the Data Card, rather than to the Data Card itself?
Yes, unless you take special notice of the results of changing the Datacard Default in relation to the Unsaved property of the form AND you utilize the Unsaved property or want to better the performance of the Submit.
When you submit a form, it will only submit fields that have changed (i.e. Patch the existing or new record with fields that have changed). In order to know what has changed, the form compares Default to Update. If there is any difference in them, then it considers that field as Changed.
The best way to look at it is:
Default is the input to your form field.
Update is the output for your form field.
What happens in between is where changes should be made. After all...that is where the user makes the changes.
NOTE: This is a general rule. Not always needed for all cases. For example there are times you have a "not visible" datacard that is always calculated based on other values in the app. In that case, altering the Update property or Default property is not a problem because you will be expecting to write it on submit anyway and no one will alter via any control....it's all calculated.
The principle is this...there are great features and functionality that the form provides, but some things you change in datacards can break those very quickly if not done right. (i.e. Valid, Unsaved, etc)
First of all, I have a Dropdown , not Combobox. Correspondingly, the property name is Default, not DefaultSelectedItems .
I have tried your approach:
Sorry, but is did not work when a Form was New. But I have found out that the following approach works OK:
See the difference? I have read from the DB 'MyTable' and this has worked. Whereas the First(Parent.AllowedValues).Value did not work: in New form the dropdown just displayed a value that had been there the previous time when the Form had been opened.
Could you please comment on that?
Keep up to date with current events and community announcements in the Power Apps community.
A great place where you can stay up to date with community calls and interact with the speakers.
Check out the latest Community Blog from the community!