@duncant , when you patch the Master record, you can actually move the entire record into a Variable. I am not in front of my work computer and apologize for not typing in the exact syntax. BUT its as simple as something like
Set(varMasterRec, Patch(MasterTable,, Deault{master table data goes here..}..)
This is akin to doing a scope_identity() in SQL Server except, that here, the variable varMasterRec will contain the just persisted, entire master row's data here.
Once the master record reference is in a variable, you can use that when you are Patching the detail records in a loop. Make sure you always alias the collection when you are doing a ForAll loop like I have done below (colDetails). This will save you tons of hardship. I once spent an hour trying to do a simple Patch Update because it doesn't understand ID=ID syntax.
So, patching the details record will be like:
,
ForAll(colDetails As 'colDetails // or galleryDetails.AllItems or whatever your details collection is)),
Patch (TableDetails, Default { MASTERCOLUMNNAMEI: varMasterRec, Rest Of the Detail Row Data}));
NOTE that, you don't have to varMasterRec.ID when patching that master column. You can directly assign the entire variable to it, as long as it is a proper lookup column to the Master row, in the back end.