Hi Leonid,
I think when I had this problem, I ended up using rawSQL ‘INSERT INTO…’ statements and then flushing caches.
Even though this might be ‘expensive’ in database terms, it is SO much faster than any other way I could find.
You can still pass parameters/arguments to the SQL statements (like your foreignValue).
It will take you a little bit of time to write the SQL and choose each column, but it is worth it.
Also, to make your code more readable, inside the SQL ‘string’ you can put a \ at the end of a line and then split the line e.g.
function archive_stock_take ()
{
// copy stock take records to archive
// Execute any SQL, returns true if successful.
var $st_no = 35 ; // this could be an argument you pass
var done = plugins.rawSQL.executeSQL ( "rnoh_estock",
"es_stock_take_archive",
"INSERT INTO es_stock_take_archive (\
date_created,\
date_edited,\
gln,\
id_created_by,\
id_edited_by,\
id_location,\
id_product,\
id_stock_item,\
id_supplier,\
line_total,\
price,\
quantity,\
stock_item_uuid_string,\
stock_take_no,\
vat_rate,\
vat\
) \
SELECT date_created,\
date_edited,\
gln,\
id_created_by,\
id_edited_by,\
id_location,\
id_product,\
id_stock_item,\
id_supplier,\
line_total,\
price,\
quantity,\
stock_item_uuid_string,\
stock_take_no,\
vat_rate,\
vat\
FROM es_stock_take ;", null ) ;
if ( done )
{
//flush is required when changes are made in db
plugins.rawSQL.flushAllClientsCache ( "rnoh_estock", "es_stock_take_archive" )
}
else
{
var msg = plugins.rawSQL.getException ( ).getMessage ( ); //see exception node for more info about the exception obj
plugins.dialogs.showErrorDialog ( 'Error', 'SQL exception: ' + msg, 'Ok' )
}
}
(this is SQL for MS SQL Server, make sure to use correct SQL syntax for your back end SQL db)
Hope this helps.
Rafi