Bulk insert array question

Hello
I´m trying to create a bulk insert GUI query, but I cannot specify correctly the required array.



The resulting array of running this is:
Array(1)
[
{
"order_header_id": 8,
"order_line_id": 26,
"account_id": [
55,
77
],
"currency": "MXN",
"credit": [
"7500.0000",
"2500.0000"
],
"debit": 0
}
]

But the correct array I would like to get is
Array(2)
[
{
order_header_id: 8,
order_line_id: 26,
account_id: 55,
currency: "MXN",
credit: "7500.0000",
debit: 0
},
{
order_header_id: 8,
order_line_id: 26,
account_id: 77,
currency: "MXN",
credit: "2500.0000",
debit: 0
}
];

Any help in correcting the "Array of records to insert" would be much appreciated. Thank you.

Hi there @luq,

What i would do is create a javascript query that prepares your data, something like:

// Assuming these are the variables
const order_header_id = {{tableOrderHeader.selectedRow?.id}};
const order_line_id = {{addNewOrderLine.data.result['0'].id}};
const account_ids = {{getOHAgentsAccount.data.account_id}};
const currency = {{tableOrderHeader.selectedRow?.currency}};
const credits = {{getFeesAgent.data.calculated_fee}};
const debit = 0;

// Initialize an empty array to hold the transformed records
const recordsToInsert = [];

// Loop through the account_ids and credits to create individual records
for (let i = 0; i < account_ids.length; i++) {
  recordsToInsert.push({
    order_header_id: order_header_id,
    order_line_id: order_line_id,
    account_id: account_ids[i],
    currency: currency,
    credit: credits[i],
    debit: debit
  });
}

// Output the resulting array
return recordsToInsert;

And use your query1.data within the "Array of records to insert" box. With this you will achieve the set of arrays regardless of how many accounts/credits you have in the respective arrays.

Hope this helps!

1 Like

Thanks! @MiguelOrtiz
It worked perfect.

1 Like