Good morning from Gran Canaria.
I´m getting crazy with DB triggers. Please could anyone tell me what is wrong with this code:
This is an onRecordInsert event on the table PROVISIONES and I´m trying to save a record in another table HISTORICO_CLIENTES. Running the code in the debugger I see all the lines are executed without problems, in fact I see that the record in HISTORICO_CLIENTES is created but only temporarily, when I check the data only the PROVISONES record is there and with the IDHISTORICO_CLIENTES field filled.
Please, what am I missing?
/**
* Record pre-insert trigger.
* Validate the record to be inserted.
* When false is returned the record will not be inserted in the database.
* When an exception is thrown the record will also not be inserted in the database but it will be added to databaseManager.getFailedRecords(),
* the thrown exception can be retrieved via record.exception.getValue().
*
* @param {JSRecord<db:/gsdespachos/provisiones>} record record that will be inserted
*
* @returns {Boolean}
*
* @properties={typeid:24,uuid:"01097AFC-A34E-4A03-BB43-D2BEB5B5CCF6"}
*/
function provisiones_onRecordInsert(record) {
// Cargamos el numero de documento
if(!record.documento){
var cSQL='SELECT MAX(documento) FROM provisiones WHERE provisiones.idinquilino=?';
var _ds=databaseManager.getDataSetByQuery('gsdespachos',cSQL,[globals.currentInquilinoID],1);
if(_ds.getMaxRowIndex()==0 || _ds.getValue(1,1)==null){
record.documento=1
}else{
record.documento=_ds.getValue(1,1)+1;
}
}
// Verificamos el estado
record.activa=1;
// Grabamos el movimiento en el histórico
/** @type {JSFoundset<db:/gsdespachos/historico_clientes>}*/
var fsHistorico=databaseManager.getFoundSet('gsdespachos','historico_clientes');
var recHistorico=fsHistorico.getRecord(fsHistorico.newRecord());
recHistorico.concepto='Su Entrega Provisión ('+globals.PADL(record.documento.toString(),10,'0')+')';
recHistorico.fecha=record.fecha;
recHistorico.haber=record.importe;
recHistorico.idcliente=record.idcliente;
recHistorico.idcliente_manual=record.idcliente_manual;
recHistorico.idprovision=record.idprovision;
databaseManager.saveData(fsHistorico);
record.idhistorico_clientes=recHistorico.idhistorico_clientes;
return true;
}
I have tried also trying to save the record with a relation provisiones_to_historico_clientes.newRecord() BLA BLA BLA, but it does not work either.
Thanks