Just sharing the sample formula, we used for associating selected products from the gallery to lead on OnSelect of a button.
Here collectionCheckedProducts contains the checked products from the gallery bound to the product data source –
varEnquiryIdWithoutCurlyBraces contains the GUID of the Lead record. Here Enquiry is Lead renamed.
Enquires(leadproduct_association) is the name of the relationship between lead and product.
Relate function is used to associate multiple selected products in collectionCheckedProducts to a lead record.
And below is the sample formula we used for creating Quote Product for selected products from the gallery.
varEnquiryIdWithoutCurlyBraces contains the GUID of the Quote record. Proposal Data Source is the Quote renamed.
Hope it helps..