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.
//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?
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
@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}
)
)
);
I amended the original posted code.
Thank you. The code had errors which I provided a screenshot. No worries.
You cannot get more concise than what I posted doing what you require.
@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 Thank you. The amended code has not been posted lol.
Hi @zoso2112 ,
Now I can test it, try the amended code.
WarrenBelz
146,524
Most Valuable Professional
RandyHayes
76,287
Super User 2024 Season 1
Pstork1
65,906
Most Valuable Professional