cancel
Showing results for 
Search instead for 
Did you mean: 

Collection - Set all field in the collection with a default value

Hi PowerApps team,

 

Please set always a default to each column of a collection!

Oke, let me try to explain. I will first scetch the situation, then give the issue I'm experiencing and a last the current workaround needed.

Situation
Let's say we have a collection for employees with multiple columns:
[ID, FirstName, LastName, DisplayName, RoleID, RoleName]
I bring in the items with Flow(), so on app start I have items in my collection. Now I want to be able to update and create items in my collection. 

 

For Update I use a Variable Record (I set this by selecting the record in a gallery with

UpdateContext({MySelectedItem:ThisItem})

This variable I use for my form. Till so far no problems. 

 

Now I make changes and want to save them in my Collection and my DataSource. For that I use an other collection _EmployeePost. What I want to do is

Set(_EmployeePostID,MySelectedItem.ID)
;Collect(_EmployeePost, MySelectedItem)
;Patch(_EmployeePost, LookUp(_EmployeePost, ID = _EmployeePostID)
	,{FirstName: "New FirstName"}
	,{LastName: "New LastName"}
	,{DisplayName: "New DisplayName"}
	,{Role: "New Role"}
)

For a new record I would like to do

Set(_EmployeePostID,Max(_Employee, ID) + 1)
;Collect(_EmployeePost, {ID:_EmployeePostID})
;Patch(_EmployeePost, LookUp(_EmployeePost, ID = _EmployeePostID)
	,{FirstName: "New FirstName"}
	,{LastName: "New LastName"}
	,{DisplayName: "New DisplayName"}
	,{Role: "New Role"}
)

Issue

The issue is that this won't work. This is because with an update if one field is blank()/Null this field is not set and I can't update the value here. On a new record you can't update all the fields besides the ID.

 

Workaround
What I have to do now is:

// Update
Set(_EmployeePostID,MySelectedItem.ID)
;Collect(_EmployeePost
	, {ID: If(IsBlank(MySelectedItem.ID),"", MySelectedItem.ID)}
	,{FirstName: If(IsBlank(MySelectedItem.FirstName),"", MySelectedItem.FirstName)}
	,{LastName: If(IsBlank(MySelectedItem.LastName),"", MySelectedItem.LastName)}
	,{DisplayName: If(IsBlank(MySelectedItem.DisplayName),"", MySelectedItem.DisplayName)}
	,{Role: If(IsBlank(MySelectedItem.Role),"", MySelectedItem.Role)}
)
;Patch(_EmployeePost, LookUp(_EmployeePost, ID = _EmployeePostID)
	,{FirstName: "New FirstName"}
	,{LastName: "New LastName"}
	,{DisplayName: "New DisplayName"}
	,{Role: "New Role"}
)

// Insert
Set(_EmployeePostID,Max(_Employee, ID) + 1)
;Collect(_EmployeePost
	, {ID:_EmployeePostID}
	,{FirstName: ""}
	,{LastName: ""}
	,{DisplayName: ""}
	,{Role: ""}
)
;Patch(_EmployeePost, LookUp(_EmployeePost, ID = _EmployeePostID)
	,{FirstName: "New FirstName"}
	,{LastName: "New LastName"}
	,{DisplayName: "New DisplayName"}
	,{Role: "New Role"}
)

You see the quantity of script and we only have a simple collection here. I hope you can imagine the time it takes to build this with a langer collection.

 

Hope you can fix this.

 

Paul

Status: New
Comments
Level 8

For me, this brings up the idea of having (optionally) a formal collection declaration, where I can specify each field, its type, and a default value. A number of times I've had something out of sync in one of my collections and it was very difficult to find the issue. It would be nice if we had the option of a formal declaration of the data type.

Level 10

@sopatte 

 

Oke, understand your point, but you should create another idea for this thread. Reason is I use flow to fill my collections on start, so the schema and right declaration of types is automatically implemented.

 

Btw you can also declare the collection by building it by collecting one line with columns and data (for number you then pit in a 0, for text "") and then clear the collection. You can do this OnStart or by a toggle you trigger from the OnStart.

 

So this request is a little of topic in this thread. So pleasemake a new idea.

 

Paul