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 / Patch a SharePoint Lis...
Power Apps
Answered

Patch a SharePoint List and Include Items in a Collection

(1) ShareShare
ReportReport
Posted on by 134

I have a canvas app that uses a SharePoint list for the data source. On the Home screen, the user selects one or more checkboxes to determine what content will be included on the form. Checking and unchecking the checkboxes adds and removes that item from the 'HmCheckboxStates' collection.

  • I add items to the 'HmCheckboxStates' collection one at a time when the user checks a checkbox.
  • I remove items one at a time when the user unchecks a checkbox.
  • As long as at least one item exists in the 'HmCheckboxStates' collection, I allow the user to submit these selections which in turn I use the Patch function to create a new record in the 'DS PreOp Insp Jobs' SharePoint list.
  • The 'HmCheckboxStates' collection has three columns: 'ListCol', 'Name', and 'Value'.
  • 'ListCol' is the actual column name in the SharePoint list.
  • 'Name' is the name of the checkbox (e.g., 'checkboxSkidSystem1')
  • 'Value' will be T/F based on the state of the checkbox
  • 'ListCol' is the name of the corresponding column in the 'DS PreOp Insp Jobs' SP list.

 

//Example: 'checkboxSkidSystem1' checkbox

// OnCheck
Collect(HmCheckboxStates, {Name: "checkboxSkidSystem1", Value: checkboxSkidSystem1.Value, ListCol: "Skid System 1"})

// OnUncheck
Remove(HmCheckboxStates, LookUp(HmCheckboxStates, Name = "checkboxSkidSystem1"))

 

I need to use the Patch function to create a new record in the 'DS PreOp Insp Jobs' SP list so I am looking at how would I loop through all items in the 'HmCheckboxStates' collection to update the corresponding column. For example, if 'checkboxSkidSystem1' is checked, then I need to create a new record and the 'Skid System 1' column (a Yes/No col type) needs to be saved as a Yes. The same goes for any other items in the 'HmCheckboxStates' collection.

 

// I need to add code to include the 'HmCheckboxStates' checkbox collection items to this code
Patch(
 'DS PreOp Insp Jobs', 
 Defaults('DS PreOp Insp Jobs'), 
 // Include the 'JobName' and 'Inspection Date Start' fields
 {
 'JobName': "PreOp-" & Text(Now(), "[$-en-US]yyyy-mm-dd hh:mm:ss"),
 'Inspection Date Start': Now()
 }
);

 

What is best for looping through the 'HmCheckboxStates' collection and setting the respective columns to Yes while allowing other columns in the destination SP list be updated?

Categories:
I have the same question (0)
  • zoso2112 Profile Picture
    134 on at

    Here is a solution that I came up with but there should be a way to make the code more concise. 

    // Create a new record in the 'DS PreOp Insp Jobs' SharePoint list
    Patch(
     'DS PreOp Insp Jobs', 
     Defaults('DS PreOp Insp Jobs'), 
     // Include the 'JobName' and 'Inspection Date Start' fields
     {
     'JobName': "PreOp-" & Text(Now(), "[$-en-US]yyyy-mm-dd hh:mm:ss"),
     'Inspection Date Start': Now()
     }
    );
    
    // Loop through the 'HmCheckboxStates' collection
    ForAll(
     HmCheckboxStates,
     // Use a separate Patch function for each checkbox state
     If(
     Name = "checkboxSkidSystem1",
     Patch(
     'DS PreOp Insp Jobs',
     Last('DS PreOp Insp Jobs'),
     {'Skid System 1': Value}
     ),
     Name = "checkboxSkidSystem2",
     Patch(
     'DS PreOp Insp Jobs',
     Last('DS PreOp Insp Jobs'),
     {'Skid System 2': Value}
     ),
     Name = "checkboxBBQLine",
     Patch(
     'DS PreOp Insp Jobs',
     Last('DS PreOp Insp Jobs'),
     {'BBQ Line': Value}
     ),
     Name = "checkboxStarchLine",
     Patch(
     'DS PreOp Insp Jobs',
     Last('DS PreOp Insp Jobs'),
     {'Starch Line': Value}
     ),
     Name = "checkboxMetalDetector",
     Patch(
     'DS PreOp Insp Jobs',
     Last('DS PreOp Insp Jobs'),
     {'Metal Detector': Value}
     ),
     Name = "checkboxPkgGallonLine",
     Patch(
     'DS PreOp Insp Jobs',
     Last('DS PreOp Insp Jobs'),
     {'Packaging Gallon Line': Value}
     ),
     Name = "checkboxPkgCartonsPails",
     Patch(
     'DS PreOp Insp Jobs',
     Last('DS PreOp Insp Jobs'),
     {'Packaging Cartons Pails Tubs': Value}
     ),
     Name = "checkboxDrumTotes",
     Patch(
     'DS PreOp Insp Jobs',
     Last('DS PreOp Insp Jobs'),
     {'Packaging Drums Totes': Value}
     )
     )
    );

    Any recommendations are appreciated. 

  • Verified answer
    WarrenBelz Profile Picture
    155,332 Most Valuable Professional on at

    Hi @zoso2112 ,

    Try this format - I assume that both Name and Value are in â€˜DS Pre Op Insp Jobs’

    ForAll(
     HmCheckboxStates As _Data,
     Patch(
     'DS PreOp Insp Jobs',
     Last('DS PreOp Insp Jobs'), 
     Switch(
     _Data.Name,
     "checkboxSkidSystem1",
     {'Skid System 1': _Data.Value},
     "checkboxSkidSystem2",
     {'Skid System 2': _Data.Value},
     "checkboxBBQLine",
     {'BBQ Line': _Data.Value},
     "checkboxStarchLine",
     {'Starch Line': _Data.Value},
     "checkboxMetalDetector",
     {'Metal Detector': _Data.Value},
     "checkboxPkgGallonLine",
     {'Packaging Gallon Line': _Data.Value},
     "checkboxPkgCartonsPails",
     {'Packaging Cartons Pails Tubs': _Data.Value},
     "checkboxDrumTotes",
     {'Packaging Drums Totes': _Data.Value}
     )
     )
    );

     

    Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

    MVP (Business Applications)   Visit my blog Practical Power Apps

  • zoso2112 Profile Picture
    134 on at

    Thanks @WarrenBelz for the code. Unfortunately, I get the errors seen below. 

    PreOp Insp App - screenshot forum.png
    I have working code but it requires multiple patches which requires additional error checks. 

  • WarrenBelz Profile Picture
    155,332 Most Valuable Professional on at

    Hi @zoso2112 ,

    Now I can test it, try the amended code.

  • zoso2112 Profile Picture
    134 on at

    @WarrenBelz  Thank you.  The amended code has not been posted lol. 

  • WarrenBelz Profile Picture
    155,332 Most Valuable Professional on at

    @zoso2112 ,

    This is now solved ?

  • zoso2112 Profile Picture
    134 on at

    @WarrenBelz - My second post is working code. I was just trying to only use a single patch.  As always, I appreciate your responses. If you feel like taking looking at a more concise solution then it is much appreciated. If not, thank you for your contributions to this thread. 

  • WarrenBelz Profile Picture
    155,332 Most Valuable Professional on at

    @zoso2112 ,

    You cannot get more concise than what I posted doing what you require.

  • zoso2112 Profile Picture
    134 on at

    Thank you. The code had errors which I provided a screenshot. No worries. 

  • WarrenBelz Profile Picture
    155,332 Most Valuable Professional on at

    @zoso2112 ,

    I amended the original posted code.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
Vish WR Profile Picture

Vish WR 893

#2
Valantis Profile Picture

Valantis 571

#3
11manish Profile Picture

11manish 482

Last 30 days Overall leaderboard