Hi,
I want to create a collection with x-amount of fields.
One of the fields might later in the app be populated by a record.
Datasource: CDS/Dataverse
If I create the collection like below, then later I try to populate the field "Author" with a Contact record I get the error further below
Collect(
colBook,
{
Title: txtInput1.Text,
Author: Blank()
}
)
Please note that above is just an example, so First(Contacts) is just for testing purpose.
I get that the types are not of the same type, so my question is:
How do I create a blank column in a collection, that's the type "Record"?
(It's not a valid scenario for me, to insert a temporary Contact, when creating the collection)
Out of curiosity, if you do this instead does it work?
Collect(
colBook,
{
Title: txtInput1.Text,
Author: {}
}
)
Or do you get the same problem?
(above is untested by us and is pseudocode - please test it and let us know if it worked)
As the comment above mentioned, "Author" expected to be a Record, so that you need to set {} instead of Blank().
Thank you @Reimi and @poweractivate
I tried out your suggestions, but it gives an error on the {} part
If I swap out {} for something else, like "test", Blank(), Now() - whatever, it doesn't give an error.
We may need to do deeper test, but the Collection schema may have been established from you playing with it before. Try changing the name of the Collection to something else and see if it works then. If that does not work let us know and maybe we will check it more deeply.
Unfortunately it gives the same result, i.e. an error the "The function 'Collect' has come invalid arguments."
We did the deeper check and it is because you are using First(Contacts).
In your case you must initialize empty record in the more complicated way and here is why. It is because a Record is kind of like an instance of Table especially the way you are using it. So in the beginning it has to have at least one column defined that matches the schema of the Contacts table from Dataverse otherwise you will get those errors.
So like this:
OnVisible Screen 1:
ClearCollect(colBook,{});
ClearCollect(_blankCollectionMock1,{firstname:"somemockvalue"});
Collect(
colBook,
{
Title: txtInput1.Text,
Author: Defaults(_blankCollectionMock1)
}
)
In above we choose firstname as a key on purpose - because it is one of the columns for Contact Dataverse table, that is why.
We tested and Defaults(_blankCollectionMock1) will return the closest you wanted to a "Blank" "Record". It would be a Record with the schema of firstname but evaluation of Defaults(_blankCollectionMock1) per total should return "nothing" in as close of a sense as possible that you wanted we believe.
We do not believe it is supported to just use {} in this case because you use First(Contacts) - because of this case the first initial "empty" record must still contain one of the columns so the schema is returned as matching so that is why just putting {} there will not work, not even as the definition of {} for the _blankCollectionMock1 part - must be like this instead from our deep testing.
Now this part, say from a Button OnSelect:
UpdateIf(colBook,true,{Author:First(Contacts)})
should be fine.
Neither should throw any error when used like this and it should meet the requirement as well because while _blankCollectionMock1 is not blank in any sense, the usage of Defaults(_blankCollectionMock1) returns "empty record"
Besides that we specifically tested it:
it also appears in the docs as well:
https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-defaults
Data sources vary in how much default information they provide, including not providing any at all. When you work with a collection or another data source that doesn't support default values, the Defaults function will return an empty record.
Check if this helps now @m_jeppesen
Actually this is very important. You need to remove from your requirement to use empty or blank. Upon further testing, our implementation above would only populate the firstname. If that is not what you want, you better do like this instead:
OnVisible Screen1:
ClearCollect(colBook,Blank());
Collect(
colBook,
{
Title: txtInput1.Text,
Author: Defaults(Contacts)
}
)
You must use Defaults(Contacts) . This will still be 'empty" as per your requirement, but it will init all the columns correctly.
So then this part (from OnSelect of a Button for example)
UpdateIf(colBook,true,{Author:First(Contacts)})
would actually populate all the columns of the initialized schema in Author as a Record using the UpdateIf.
See if this helps @m_jeppesen
@m_jeppesen
Collections cannot be initialized with blank and then later on be used to store a value.
To solve your issue, simply put this code in the OnStart property of the app. When the app opens it will define the schema of the collection and then immediately remove its contents. You will no longer get the ObjNull error.
ClearCollect(
colBook,
{
Title: txtInput1.Text,
Author: First(Contacts)
}
);
Clear(colBook);
---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."
The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.
User | Count |
---|---|
207 | |
98 | |
60 | |
55 | |
52 |
User | Count |
---|---|
257 | |
160 | |
87 | |
79 | |
66 |