Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Building Power Apps
Answered

Using ForAll and LookUp to Patch SharePoint list

(0) ShareShare
ReportReport
Posted on by 446

I have a collection of data that I will be uploading to a SharePoint list. I want to first check to see if the first two columns will match an existing row in that list. If it does then I want it to do nothing, otherwise it will patch the new record. I feel like the solution here is to use a combination of ForAll, LookUp, and Patch, but for the life of me I cannot get it to work.

 

 

ForAll(
 colFinal,
 If(
 LookUp(Acronyms, ThisRecord.Acronym = Acronym && ThisRecord.Definition = Definition, true),
 "",
 Patch(
 'Acronym Submissions', 
 Defaults('Acronym Submissions'),
 {
 Acronym: ThisRecord.Acronym,
 Definition: ThisRecord.Definition
 }
 )
 )
);

 

 

The error I'm getting when using the above code is that it's expecting a Text value instead of a record in the Patch function. After doing some research I found others had the same issue, but they were performing the LookUp on a single piece of information within the record rather than the entire record itself. I have a feeling that I have the order wrong or the entire concept wrong, as the problem lies with the ThisRecord portion in the Patch.

 

I'm not sure what I'm doing wrong as I feel like I've done something similar to this before.

Categories:
  • Ryan_B Profile Picture
    446 on at
    Re: Using ForAll and LookUp to Patch SharePoint list

    @RandyHayes 

     

    I see now. You're right, I'm trying to treat it like a ForLoop which makes sense to me. This was a much smoother approach and works flawlessly. Thank you for your help and expertise as always!!

  • Verified answer
    RandyHayes Profile Picture
    76,287 Super User 2024 Season 1 on at
    Re: Using ForAll and LookUp to Patch SharePoint list

    @Ryan_B 

    Well, to start, your formula has the ForAll backward. You are trying to use it like a ForLoop in some development language - which PowerApps is not.  It is a function that returns a table of records based on your iteration table and record schema.

    It is more efficient to use the function as intended and will provide better performance.

     

    Your formula should be the following:

    Patch('Acronym Submissions', 
     Filter(
     ForAll(colFinal As _item,
     If(!LookUp(Acronyms, _item.Acronym = Acronym && _item.Definition = Definition, true),
     {
     Acronym: _item.Acronym,
     Definition: _item.Definition
     }
     )
     ),
     !IsBlank(Acronym)
     )
    );

     

    Patch is smart...first, it takes a table (which is what ForAll was designed to create - a table) and will use that to perform the patching from the entire table - not instantiating patch over and over like when you use the ForAll backward.

    Also, it is smart because if you include the primary key in your records, it will then update that record.  If the primary key is missing, then it will create a record.

     

    The filter in the above is to ensure that nothing that no blank records are attempted to be patched.

     

    The above formula should provide what you need.

     

    I hope this is helpful for you.

  • Ryan_B Profile Picture
    446 on at
    Re: Using ForAll and LookUp to Patch SharePoint list

    Update:

     

    I altered the code slightly to what's found below by adding an IsBlank and the LookUp returning the record instead of true. No errors this time but something still isn't right. I attempted to add a duplicate record and a new one, but both went through.

    ForAll(
     colFinal,
     If(
     IsBlank(
     LookUp(Acronyms, ThisRecord.Acronym = Acronym && ThisRecord.Definition = Definition, ThisRecord)),
     Patch(
     'Acronym Submissions', 
     Defaults('Acronym Submissions'),
     {
     Acronym: ThisRecord.Acronym,
     Definition: ThisRecord.Definition
     }
     )
     )
    );

     

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,651 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 65,999 Most Valuable Professional

Leaderboard