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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / ForAll Patch Lookup up...
Power Apps
Suggested Answer

ForAll Patch Lookup updates only the first record in the table

(2) ShareShare
ReportReport
Posted on by 153
I have a statement where I want to patch a SQL Server table from a collection in Power Apps. 
 
 
ForAll(
    colModifiedUserResponesWeather,
    Patch(
        'ERM.UserResponses',// SQL Server table data source
        LookUp(
            'ERM.UserResponses',
            UserResponseId = ThisRecord.UserResponseId
        ),// Find matching row in SQL
        {
            Comment: ThisRecord.Comment,
            IsComplete: ThisRecord.IsComplete,
            Modified: ThisRecord.Modified,
            Modifier: ThisRecord.Modifier,
            ResponseValue: ThisRecord.ResponseValue,
            ScoringGuideId: ThisRecord.ScoringGuideId
        }
    )
);
The problem is that the LookUp is not working, it is grabbing the first record from the table and updating it multiple times.
Categories:
I have the same question (0)
  • Suggested answer
    Valantis Profile Picture
    5,977 on at
     
    The issue is a scope conflict. Inside ForAll, when you use LookUp against an external data source like SQL Server, Power Apps can lose the ThisRecord context and default to returning the first record.
    The fix is to store the ID in a local variable before the LookUp:
    ForAll(
        colModifiedUserResponesWeather,
        With(
            { _id: ThisRecord.UserResponseId },
            Patch(
                'ERM.UserResponses',
                LookUp('ERM.UserResponses', UserResponseId = _id),
                {
                    Comment: ThisRecord.Comment,
                    IsComplete: ThisRecord.IsComplete,
                    Modified: ThisRecord.Modified,
                    Modifier: ThisRecord.Modifier,
                    ResponseValue: ThisRecord.ResponseValue,
                    ScoringGuideId: ThisRecord.ScoringGuideId
                }
            )
        )
    )
    The With() captures ThisRecord.UserResponseId into _id before Power Apps tries to resolve the LookUp against SQL Server. This forces the correct ID to be used for each iteration.
     

     

    Best regards,

    Valantis

     

    ✅ If this helped solve your issue, please Accept as Solution so others can find it quickly.

    ❤️ If it didn’t fully solve it but was still useful, please click “Yes” on “Was this reply helpful?” or leave a Like :).

    🏷️ For follow-ups  @Valantis.

    📝 https://valantisond365.com/

    💼 LinkedIn

    ▶️ YouTube

  • Suggested answer
    11manish Profile Picture
    2,635 on at
    Please try below :
    ForAll(
        colModifiedUserResponesWeather As colRec,
        Patch(
            'ERM.UserResponses',
            LookUp(
                'ERM.UserResponses',
                UserResponseId = colRec.UserResponseId
            ),
            {
                Comment: colRec.Comment,
                IsComplete: colRec.IsComplete,
                Modified: colRec.Modified,
                Modifier: colRec.Modifier,
                ResponseValue: colRec.ResponseValue,
                ScoringGuideId: colRec.ScoringGuideId
            }
        )
    )
     
  • tjestesjr Profile Picture
    153 on at
    The With statement is producing an error:
     
  • Suggested answer
    SpongYe Profile Picture
    5,883 Super User 2026 Season 1 on at
     
    Valantis is on the right track. I would suggest fine-tuning the code by explicitly aliasing both record scopes. This helps avoid ambiguity between the current row in the collection and the current row being evaluated in SQL Server.
     
    ForAll(
        colModifiedUserResponesWeather As colRow,
        Patch(
            'ERM.UserResponses',
            LookUp(
                'ERM.UserResponses' As sqlRow,
                sqlRow.UserResponseId = colRow.UserResponseId
            ),
            {
                Comment: Coalesce(colRow.Comment, ""),
                IsComplete: colRow.IsComplete,
                Modified: colRow.Modified,
                Modifier: colRow.Modifier,
                ResponseValue: colRow.ResponseValue,
                ScoringGuideId: colRow.ScoringGuideId
            }
        )
    );
    Using colRow and sqlRow makes the intended scope explicit, so the LookUp() should match the correct SQL row instead of repeatedly updating the first record.
     

    If you have any questions or feedback, please let me know. Have a great day! 😊

    -----------------------
    SpongYe Power Platform Enthusiast [LinkedIn] | [Youtube| [My blog]

     

    I love to share my knowledge and learn from others. If you find my posts helpful, please give them a thumbs up 👍 or mark them as a solution ✔️. You can also check out my blog [@SpongYe] for some cool solutions and insights. Feel free to connect with me on any of the platforms above. Cheers! 🍻

  • Suggested answer
    RaghavMishra Profile Picture
    97 on at
    Hi tjestesjr,
     
    Building on the great suggestions from Valantis and SpongYe - here is the Microsoft Learn context explaining exactly why the As operator aliasing fixes this.
     
    The ForAll function documentation notes that when you have nested functions like LookUp inside ForAll iterating over the same data source, there can be scope ambiguity. Both the outer ForAll loop and the inner LookUp are referencing ERM.UserResponses, so Power Apps may resolve the UserResponseId field against the wrong scope, causing the LookUp to always match the first record.
     
    The As operator lets you explicitly name each record scope to eliminate this ambiguity:
     
    ForAll(colModifiedUserResponesWeather As colRow,
      Patch('ERM.UserResponses',
        LookUp('ERM.UserResponses' As sqlRow,
          sqlRow.UserResponseId = colRow.UserResponseId),
        {
          Comment: Coalesce(colRow.Comment, ""),
          IsComplete: colRow.IsComplete,
          Modified: colRow.Modified,
          Modifier: colRow.Modifier,
          ResponseValue: colRow.ResponseValue,
          ScoringGuideId: colRow.ScoringGuideId
        }
      )
    )
     
    With colRow and sqlRow as explicit aliases, Power Apps knows exactly which record scope each field reference belongs to, so the LookUp correctly matches each SQL row instead of repeating the first.
     
    References:
     
    Found this helpful? Please mark "Does this answer your question?" so others searching for the same issue can find it quickly. A thumbs up on "Was this reply helpful?" or a Like is also much appreciated!
     
    Raghav Mishra — LinkedIn | PowerAI Labs

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
Vish WR Profile Picture

Vish WR 414

#2
Valantis Profile Picture

Valantis 387

#3
timl Profile Picture

timl 344 Super User 2026 Season 1

Last 30 days Overall leaderboard