Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Building 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:
  • zoso2112 Profile Picture
    134 on at
    Re: Patch a SharePoint List and Include Items in a Collection

    @WarrenBelz - Thank you sir!

  • Verified answer
    WarrenBelz Profile Picture
    146,524 Most Valuable Professional on at
    Re: Patch a SharePoint List and Include Items in a Collection

    @zoso2112 ,

    Probably the most reliable way would be this

    UpdateContext(
     {
     _ID:
     Patch(
     'DS PreOp Insp Jobs', 
     Defaults('DS PreOp Insp Jobs'), 
     {
     JobName: "PreOp-" & varNowDateTime,
     'Inspection Date Start': Now(),
     JobDocNum: "D 11.2.5.7.i FORM",
     JobRevNum: "1.7"
     }
     ).ID
     }
    );
    ForAll(
     HmCheckboxStates As _Data,
     Patch(
     'DS PreOp Insp Jobs',
     {ID: _ID}, 
     Switch(
     . . . . .

     

    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
    Re: Patch a SharePoint List and Include Items in a Collection

    @WarrenBelz - Thanks so much! I didn't realize you had amended your original code. It works now! Here is the new code.  Is there a way to combine these into a single Patch? If not, how would handle errors? 

    // 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-" & varNowDateTime,
     'Inspection Date Start': Now(),
     JobDocNum: "D 11.2.5.7.i FORM",
     JobRevNum: "1.7"
     }
    );
    
    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}
     )
     )
    );

     

  • WarrenBelz Profile Picture
    146,524 Most Valuable Professional on at
    Re: Patch a SharePoint List and Include Items in a Collection

    @zoso2112 ,

    I amended the original posted code.

  • zoso2112 Profile Picture
    134 on at
    Re: Patch a SharePoint List and Include Items in a Collection

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

  • WarrenBelz Profile Picture
    146,524 Most Valuable Professional on at
    Re: Patch a SharePoint List and Include Items in a Collection

    @zoso2112 ,

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

  • zoso2112 Profile Picture
    134 on at
    Re: Patch a SharePoint List and Include Items in a Collection

    @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
    146,524 Most Valuable Professional on at
    Re: Patch a SharePoint List and Include Items in a Collection

    @zoso2112 ,

    This is now solved ?

  • zoso2112 Profile Picture
    134 on at
    Re: Patch a SharePoint List and Include Items in a Collection

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

  • WarrenBelz Profile Picture
    146,524 Most Valuable Professional on at
    Re: Patch a SharePoint List and Include Items in a Collection

    Hi @zoso2112 ,

    Now I can test it, try the amended 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

🌸 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,524 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 65,906 Most Valuable Professional

Leaderboard