web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Patching an existing r...
Power Apps
Unanswered

Patching an existing record in a Dataverse table from within a Forall

(0) ShareShare
ReportReport
Posted on by 41

I have a collection from a Dataverse table; 'Tickets'

 

ClearCollect(col_TicketsToBeRedeemed,
FirstN( Sort( Filter( Tickets, Redeemed_Date = Blank() And Ticket_Owner = "John@Smith.com"),
Issued_Date, Ascending), 1 ));

 

 With this collection 'col_TicketsToBeRedeemed', I need to loop through each individual record and update a field called 'Redeemed_Date' in the DB table.

 

ForAll( col_TicketsToBeRedeemed, 
 Patch( Tickets,
 LookUp( Tickets, ThisRecord.Id = col_TicketsToBeRedeemed[@Id]),
 { Redeemed_Date: Now() }
 )
);

 

The initial collection 'col_TicketsToBeRedeemed' was created from the same table I am attempting to Patch to. 'Tickets'

The column headers in the collection and table are the same.

The field 'Id' is a Autonumbered field type.

There is a Unique Identifier field 'ticketsid' which I've also attempted to match on but no luck.

I have tried many many different iterations of this in hopes of getting to work without success.

 

Any assistance would be much appreciated.

 

 

 

Categories:
I have the same question (0)
  • MichelK Profile Picture
    120 on at

    Hello @Slevin3900 ,

    First of all, in the following expression :

    ClearCollect(col_TicketsToBeRedeemed,
    FirstN( Sort( Filter( Tickets, Redeemed_Date = Blank() And Ticket_Owner = "John@Smith.com"),
    Issued_Date, Ascending), 1 ));

    The FirstN( Collection, 1 ) returns only one record. So if the objective is to update just the first one, there would not be a need for a ForAll, and you would just use the following code:

    Patch(Tickets,First( Sort( Filter( Tickets, IsBlank(Redeemed_Date) And Ticket_Owner = "John@Smith.com"),
    Issued_Date, Ascending))), {Redeemed_Date : Now()});

    However, if you need to update all the matching records and the FirstN was unintentional, you could use an UpdateIf :

    UpdateIf( Tickets,  IsBlank(Redeemed_Date) And Ticket_Owner = "John@Smith.com", {Redeemed_Date: Now()});

     

    Let me know if this helps! 

  • MF-11091236-0 Profile Picture
    41 on at

    I appreciate the response, the FirstN is intentional.  There could be multiple rows matching but only need to tag 'x'  number as being used.  In the filter provided some of the data is hardcoded where otherwise it would be dynamic.

    The goal of the filter is to retrieve 'x' number of the oldest rows for an individual.

    I have attempted the UpdateIf but unable to get 'x' number of rows which is important.

  • MichelK Profile Picture
    120 on at

    Deal
    Then can you try the following?

     

    ClearCollect(col_TicketsToBeRedeemed,
    FirstN( Sort( Filter( Tickets, Redeemed_Date = Blank() And Ticket_Owner = "John@Smith.com"),
    Issued_Date, Ascending), 1 ));
    UpdateIf( col_TicketsToBeRedeemed, true, { Redeemed_Date: Now() } );
    Patch( Tickets, col_TicketsToBeRedeemed ); //Could be Collect instead of Patch, forgot which one does the job

     

  • Verified answer
    MF-11091236-0 Profile Picture
    41 on at

    @MichelK  I figured out it.  The column names causing the grief and by correcting I was able to successfully get to work.

    ClearCollect(col_TicketsToBeRedeemed, 
     FirstN( Sort(
     Filter( Tickets, Redeemed_Date = Blank() And Ticket_Owner = varCustomer.Email ), 
     Issued_Date, Ascending), varCustomer.QuantityToRedeem )
    );
    ForAll(col_TicketsToBeRedeemed As Row,
     Patch( Tickets, LookUp( Tickets, Id = Row.Id),
     { Redeemed_Date: Now() } );
    )

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 717 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 329 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard