cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
tburnsJEB
Frequent Visitor

Patching new items and updates from a Collection back to DataVerse.

I have an app that is drawing from multiple Dataverse tables. To unify what needs to be I created collections out of each Table.

  • ProjectsCol
  • TasksCol
  • ApplicationsCol

I then combine these Collections into one Collection for Sorting, Filtering, Searching, etc., but when it comes to making changes they are all updated to the separate Collections since they are duplicates of the Data Source, and the CombinedCol is strictly used for what I just said and to Control the selection of the other Galleries and so forth.

 

I am able to Patch the Collections. Text, Choices, LookUp columns, and so on, but when I attempt to Patch back to the actual Datasource I can Patch New entries, but It errors when I attempt to change existing entries.

 

Here is my test run on a non-production table.

 

Here is the Collection build.

ClearCollect(
TestProjectsCol,
ShowColumns(
Filter(
'Project Portals','Project Portals (Views)'.'Active Project Portals'
),
"cr577_projectid",
"cr577_projectaddress",
"cr577_projectnotes",
"cr577_projectstatus",
"cr577_Assigned",
"cr577_projectname"
)
);

 

For now this collection is being Patched from a couple of Text Imputs and a Combo Box.

Here is the Patch that works for adding to one of the Collections:

Patch(
TestProjectsCol,
If(
IsBlank(varPick),
Defaults(TestProjectsCol),
varPick
),
{
cr577_projectnotes: TestNotes.Text,
cr577_projectid: testID.Text,
cr577_projectname: TestName.Text,
cr577_Assigned:TestAssigned2.Selected

}
)

 

I am using a variable to link the input boxes and control if the inputs show the selected Gallery item or are blank from clicking a button I use to run a UpdateContext({varPick: Blank()}) for entering New entries. The above Patch accounts for if there is data or not by running an If() against varPick.

 

Now when it comes to Patching back to the Datasource I have tried a few methods.

  • A direct Patch since the columns are identical. Works for new entries, but not existing ones.
  • A Patch built like the one above for Patching the Collection but again works for the New Entries and errors if I update existing ones.
  • Finally, a rewrite using a lookup for the Assigned LookUp column, and that method just outright fail. Does not even write to the Collection when done that way.

Here is the current Patch to the Datasource that will only write new entries, but not updates.

Patch(
'Project Portals',
If(
IsBlank(varPick),
Defaults(TestProjectsCol),
varPick
),
{
cr577_projectnotes: TestNotes.Text,
cr577_projectid: testID.Text,
cr577_projectname: TestName.Text,
cr577_Assigned:TestAssigned2.Selected

}
);
Refresh('Project Portals')

 

I know I am missing something obvious, so any assistance that anyone can render will be much appreciated. Thanks in advance.

2 REPLIES 2
Drrickryp
Super User
Super User

@tburnsJEB 

Your If() function does not contain an alternate if varPick is not blank.  The condition for determining which patch to use should be outside of the patch itself.  I arbitrarily chose projectid for the LookUp but any column containing a unique value can be used. 

 

With({patch1:Patch('Project Portals',Defaults(TestProjectsCol),
                               {
                                 cr577_projectnotes: TestNotes.Text,
                                 cr577_projectid: testID.Text,
                                 cr577_projectname: TestName.Text,
                                 cr577_Assigned:TestAssigned2.Selected
                                }
                 ),
             patch2:Patch('Project Portals',LookUp(TestProjectsCol,cr577_projectid=TextID.Text),
                               {
                                 cr577_projectnotes: TestNotes.Text,
                                 cr577_projectid: testID.Text,
                                 cr577_projectname: TestName.Text,
                                 cr577_Assigned:TestAssigned2.Selected
                                }
              )
      },
  ForAll(
          TestProjectsCol,
           If(
               IsBlank(varPick),patch1,
                                patch2
           )
  )
)

 

 

 

 

If varPick is not blank the Datasource should be updated with the contents of the field. VarPick is the variable I am using instead of Gallery.Selected. I have changed the format a little so I will try your solution with that setup since I am having the same issue.

Helpful resources

Announcements
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (5,047)