cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
EpicTriffid
Resolver I
Resolver I

Updating multiple records based on single ID

I have absolutely no idea why this isn't working.

 

I have a single person's ID number, and am trying to use that to update a field of all records with that same ID in the datasource.

 

For example: I have Joe Bloggs, whose department is currently Admin. His main record is held in my Staff Table with this current dept:

 

NameDeptUserID
Joe BloggsAdmin1234

 

I then have a sub table that lists all Joe trips in this year:

 

NameDeptUserIDTrip Destination
Joe BloggsAdmin1234Paris
Joe BloggsAdmin1234London
Joe BloggsAdmin1234Madrid
Joe BloggsAdmin1234The Moon

 

Now, Joe has left the Admin department, and gone to Sales. I need to update both the Department field in my Staff table, and at the same time, update all the records belonging to Joe in the Trips table to also have Sales as their department. Changing the Staff table, where I have a gallery and a patch function is fine. The sub table is the issue. When I change his department, I save the chosen department to a variable called chosenDept (in this case "Sales"). I then need to apply that same choice to all records in the Trips table. My Trips table is really quite large, so I have to filter the datasource down so delegation doesn't become an issue.

 

I'm using this code:

 

ClearCollect(recordToChange,Filter('[dbo].[Trips]', UserID = ThisItem.UserID));

ForAll(recordToChange,
Patch(Filter('[dbo].[Trips]', UserID = ThisItem.UserID),
    LookUp(
        '[dbo].[Trips]', ID = recordToChange[@ID]),
    {Department: First(chosenDept).Value}));

 

My Staff table updates fine, the variable reads "Sales", but the Department field in the Trips table just doesn't change at all. I'm not getting delegation errors or anything. It just does not work. Have I missed out something fundamental?

2 ACCEPTED SOLUTIONS

Accepted Solutions
RandyHayes
Super User
Super User

@EpicTriffid 

Consider changing your formula to the following:

UpdateIf('[dbo].[Trips]', 
         UserID=ThisItem.UserID,
         {Department: First(chosenDept).Value}
)

 

That should give you what you need.

 

I hope this is helpful for you.

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

v-qiaqi-msft
Community Support
Community Support

Hi@EpicTriffid,

Based on the issue that you mentioned, do you want to patch all the records related to Joe based on the UserID?

Could you please share more about the scenario, please show me how you create the variable?

Most often, we use Patch() to update a single record. It isn't commonly used to update multiple records and the usage of Patch() to update multiple records looks like this:
Patch( DataSource, BaseRecordsTable, ChangeRecordTable1)
@RandyHayes  provides a correct direction that UpdateIf() formula will be more efficient in this case.
Further, I think 

I have a test on my side with your Patch(), please modify your formula as below.

 

ClearCollect(recordToChange,Filter('[dbo].[Trips]', UserID = ThisItem.UserID))
ClearCollect(ChangeRecordsTable, 
             UpdateIf(recordToChange,UserID = ThisItem.UserID, {Dept:"Sales"})
);
Patch('[dbo].[Trips]',recordToChange, ChangeRecordsTable)

 

Best Regards,

Qi

Best Regards,
Qi

View solution in original post

2 REPLIES 2
RandyHayes
Super User
Super User

@EpicTriffid 

Consider changing your formula to the following:

UpdateIf('[dbo].[Trips]', 
         UserID=ThisItem.UserID,
         {Department: First(chosenDept).Value}
)

 

That should give you what you need.

 

I hope this is helpful for you.

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!
v-qiaqi-msft
Community Support
Community Support

Hi@EpicTriffid,

Based on the issue that you mentioned, do you want to patch all the records related to Joe based on the UserID?

Could you please share more about the scenario, please show me how you create the variable?

Most often, we use Patch() to update a single record. It isn't commonly used to update multiple records and the usage of Patch() to update multiple records looks like this:
Patch( DataSource, BaseRecordsTable, ChangeRecordTable1)
@RandyHayes  provides a correct direction that UpdateIf() formula will be more efficient in this case.
Further, I think 

I have a test on my side with your Patch(), please modify your formula as below.

 

ClearCollect(recordToChange,Filter('[dbo].[Trips]', UserID = ThisItem.UserID))
ClearCollect(ChangeRecordsTable, 
             UpdateIf(recordToChange,UserID = ThisItem.UserID, {Dept:"Sales"})
);
Patch('[dbo].[Trips]',recordToChange, ChangeRecordsTable)

 

Best Regards,

Qi

Best Regards,
Qi

Helpful resources

Announcements
Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Difinity Conference 2022

Difinity Conference 2022

Register today for two amazing days of learning, featuring intensive learning sessions across multiple tracks, led by engaging and dynamic experts.

European SharePoint Conference

European SharePoint Conference

The European SharePoint Conference returns live and in-person November 28-December 1 with 4 Microsoft Keynotes, 9 Tutorials, and 120 Sessions.

Power Apps Ideas

Changes to Ideas Coming

We are excited to announce a new way to share your ideas for Power Apps!

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