cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
m_jeppesen
Helper I
Helper I

Collection with blank field value of Record type

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()
    }
)

 m_jeppesen_0-1608638225126.png

 

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)

8 REPLIES 8
poweractivate
Community Champion
Community Champion

@m_jeppesen 

 

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)

Reimi
Community Support
Community Support

As the comment above mentioned, "Author" expected to be a Record, so that you need to set {} instead of Blank().

m_jeppesen
Helper I
Helper I

Thank you @Reimi and @poweractivate 
I tried out your suggestions, but it gives an error on the {} part

 

m_jeppesen_0-1608662815520.png

 

If I swap out {} for something else, like "test", Blank(), Now() - whatever, it doesn't give an error.

@m_jeppesen 

 

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."

@m_jeppesen 

 

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:

 

colb01.png

 

colb02.png

 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 

poweractivate
Community Champion
Community Champion

@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 

mdevaney
Super User
Super User

@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." 

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Solution Authors
Top Kudoed Authors
Users online (1,777)