I am trying to do a bulk insert query, but I need to make my own array of records to insert and I am having trouble getting this all wired together correctly.
I have two table components that I need to pull data from to create a new array to insert into a third database table. The first of the tables is multiselect so .selectedRow.data is an array.
I fire a js query from a button:
//jsAddProductsToTemplate
jsMakeBulkInsert.trigger()
qryTemplateItemsBulkInsert.trigger()
This runs jsMakeBulkInsert first which makes the array and stores it in a temp state var and then fires the SQL query.
// jsMakeBulkInsert
var newData = [];
tblProducts.selectedRow.data.forEach(row => {
newData.push({sku: row.sku, template_id: tblTemplates.selectedRow.data.template_id})
});
BulkInsertRows.setValue(newData);
The sql query then just looks like:
// qryTemplateItemsBulkInsert - Array of records to insert
{{BulkInsertRows.value}}
However, BulkInsertRows is getting set after qryTemplateItemsBulkInsert is triggered.
Browser console:
[onActionButtonClick] running an action jsAddProductsToTemplate
app.ab4664be047c4caa9cbf.js:2 JSCode triggering jsMakeBulkInsert {additionalScope: {…}}
app.ab4664be047c4caa9cbf.js:2 JSCode triggering qryTemplateItemsBulkInsert undefined
app.ab4664be047c4caa9cbf.js:2 Setting value of BulkInsertRows to (2) [{…}, {…}]
I first tried using the return value in jsMakeBulkInsert like this:
//jsMakeBulkInsert
... Same as before
return newData
and
// qryTemplateItemsBulkInsert - Array of records to insert
{{jsMakeBulkInsert.data}}
But that worked sometimes and not others and when it didn't I got an error on the order of cannot use .map in undefined.
If there is a way to force the temp state var assignment to happen when you think it will, that would solve the problem I believe.
Or is there another best practice for this?