Table Events Order

Questions, tips and tricks and techniques for scripting in Servoy

Table Events Order

Postby udiluca » Wed Feb 17, 2016 1:10 pm

Hi,

Is there a specific order in the sequence of the table events ?

i.e a foundset with new records and changed records.

Are all new records inserted to the db then all changes committed ?
Are records inserted and updated based on the foundset sort ?
Are all changes committed before and then all new records inserted ?

I'm trying to determine the best scenario to check for duplicate records within a foundset, when this foundset has new and changed records. My db events are not tied to forms foundsets or related foundsets, all is done within queries so new records are currently unreachable when controling duplicates within onRecordUpdate()

In either case I 'd check duplicates by looping through the existing parent children records, checking record.getChangedDatas() for each record in the foundset before committing this new record.

Thanks

Ugo
udiluca
 
Posts: 24
Joined: Wed Apr 10, 2013 7:25 pm

Re: Table Events Order

Postby jcompagner » Wed Feb 17, 2016 4:51 pm

we try to do it based on order or creation/updates
and after that we do try to order new record based on the pk (so if others have key constraints to that new record then the new record is done first)
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8829
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet

Re: Table Events Order

Postby udiluca » Wed Feb 17, 2016 5:45 pm

Thanks,

So if i want my checkduplicate method to succeed, I should make sure that all new records are committed before the edited records when running my parent/child save in the UI as :

Code: Select all
/**
* @param {JSEvent} event
*
* @properties={typeid:24,uuid:"782B9141-9AC4-4AF5-90BE-87B0416F348A"}
*/
function onActionSaveRecordChanges(event){

   /**@type{String}*/
   var frmEvent;
   /**@type{JSFoundset}*/
   var fsFormFoundset;
   /**@type{JSRecord}*/
   var rcParentRecord;   
   /** @type {{result:Boolean, message:String, failedrecords:JSRecord, htmlreport:String, exceptions:Array<ServoyException>}} */
   var oGetResult;
   /** @type {String} */
   var sDialog;
   /**@type{Boolean}*/
   var bHasnewrecords;
   /**@type{JSFoundSet}*/
   var fsRelated;
   /**@type{JSFoundSet}*/
   var fsNewRelated;   
   
   frmEvent = event.getFormName();
   fsFormFoundset=forms[frmEvent].foundset;
   rcParentRecord = fsFormFoundset.getSelectedRecord();
   fsRelated= rcParentRecord.parent_to_child;   
   

   //Saving main parent records if new record

   if(rcParentRecord.isNew()){
      oGetResult= globals.traSaveFoundsetRecord(rcParentRecord);
      if(!oGetResult.result){
         
         if(oGetResult.htmlreport!=null){
            //show Error Report in new Window
         }
         else{
            sDialog= globals.DIALOGS.showErrorDialog('Error',oGetResult.message,'Ok');
            onActionRollBackRecordChanges(event);
            return
         }
      }         
   }
   
   
   //Saving any new related datas
   bHasnewrecords= databaseManager.hasNewRecords(fsRelated);
   if(bHasnewrecords){
      fsNewRelated=fsRelated.duplicateFoundSet();
      fsNewRelated=globals.traOmitExistingRecords(fsNewRelated);
      oGetResult= globals.traSaveRecordsInFoundset(fsNewRelated);
      if(!oGetResult.result){
         
         if(oGetResult.htmlreport!=null){
            //show Error Report in new Window
         }
         else{
            sDialog= globals.DIALOGS.showErrorDialog('Error',oGetResult.message,'Ok');
            onActionRollBackRecordChanges(event);
            return
         }
      }
   }

   
   //General saving, takes care of any edits on existing records or/and in-memory deletes ( omitted )
   oGetResult= globals.traSaveGeneral(rcParentRecord,fsRelated);

   if(!oGetResult.result){
      
      if(oGetResult.htmlreport!=null){
         if(oGetResult.htmlreport!=null){
            //show Error Report in new Window
         }
         else{
            sDialog= globals.DIALOGS.showErrorDialog('Error',oGetResult.message,'Ok');
            onActionRollBackRecordChanges(event);
            return
         }
      }
      else{
         sDialog= globals.DIALOGS.showErrorDialog('Error',oGetResult.message,'Ok');
         onActionRollBackRecordChanges(event);
         return
      }
   }
   else{
      _super.onActionSaveRecordChanges(event);
   }
}


Am i correct thinking that this way I can use the same global method not tied to any related foundset to check for duplicate entries within the OnRecordinsert/onRecordUpdate :
- getting the related foundset from a Query on the parent pk
- looping through records to check the record.getDataChanges() if this record is not already included in the omitted array ( records omitted that should be deleted when saving the parent record )
- checking through these changes if the new inserted record may be a duplicate ( same foreign keys associated )

Edit : should I read your answer "we try to do it based on order or creation/updates" as "we try to do it based on order of creation/updates" which then mean that new related records are treated before edited records in the related foundset ?
udiluca
 
Posts: 24
Joined: Wed Apr 10, 2013 7:25 pm


Return to Methods

Who is online

Users browsing this forum: No registered users and 9 guests