Happy New years to all,
I have the following save method that informs me in the event of error:
if ( examiner != null & wsc_date != null)
{ forms.beh_wisc_iii.controller.newRecord()
forms.beh_wisc_iii.subj_num = subj_num
forms.beh_wisc_iii.wsc_date = wsc_date
forms.beh_wisc_iii.examiner = examiner
forms.beh_wisc_iii.validated = validated
forms.beh_wisc_iii.wcs_coding_age = wcs_coding_age
forms.beh_wisc_iii.wcs_coding_raw = wcs_coding_raw
forms.beh_wisc_iii.wsc_age = wsc_age
forms.beh_wisc_iii.wsc_comp_age = wsc_comp_age
forms.beh_wisc_iii.wsc_which_study = wsc_which_study
databaseManager.saveData();
//Get the failed records after a save
var array = databaseManager.getFailedRecords()
for( var i = 0 ; i < array.length ; i++ )
{
var record = array[i];
var alert = application.output(record.exception);
var alert = plugins.dialogs.showErrorDialog( "Error", "Error saving record. Please check all the fields", 'OK');
databaseManager.rollbackEditedRecords()
}
}
But I am not able to make it inform me of when the record was saved.:
else
{
plugins.dialogs.showInfoDialog( "Hi", "Record was saved", 'Ok')
}
Thanks for any help ![Smile :)]()
Hi Abrahim,
Looking at your post and piecing the code sections together your process seems to boil down to the following:
if ( there is data in examiner and date )
{ add new record
save data
get failed records
loop through failed records and show error
}
else
{
plugins.dialogs.showInfoDialog( "Hi", "Record was saved", 'Ok')
}
So it seems that the only time that it will show your success dialogue is when there is nothing in the examiner and date fields as the else condition has nothing to do with the branch that handles the save of the data
If that’s the case then that’s where your code is going wrong as you need to nest another ‘If…Else’ statement within the add record branch to handle the save or failure to save so it looks more like this:
if ( there is data in examiner and date )
{ add new record
save data
If (there are failed records)
{get failed records
loop through failed records and show error
}
else
{
plugins.dialogs.showInfoDialog( "Hi", "Record was saved", 'Ok')
}
}
else
{
plugins.dialogs.showInfoDialog( "Can't add record", "No data in examiner and date", 'Ok')
}
Cheers
Harry
Hi Harry,
With your suggestion help I came up with the following:
var MSG = ""
if ( examiner != null & wsc_date != null)
{ forms.beh_wisc_iii.controller.newRecord()
forms.beh_wisc_iii.subj_num = subj_num
forms.beh_wisc_iii.wsc_date = wsc_date
forms.beh_wisc_iii.examiner = examiner
forms.beh_wisc_iii.validated = validated
forms.beh_wisc_iii.wcs_coding_age = wcs_coding_age
forms.beh_wisc_iii.wcs_coding_raw = wcs_coding_raw
forms.beh_wisc_iii.wsc_age = wsc_age
forms.beh_wisc_iii.wsc_comp_age = wsc_comp_age
forms.beh_wisc_iii.wsc_which_study = wsc_which_study
databaseManager.saveData();
var array = databaseManager.getFailedRecords()
if (array == '[]')
{ plugins.dialogs.showInfoDialog( "Hi", "Record was saved", 'Ok')
}
else (array != '[]')
{
var array = databaseManager.getFailedRecords()
for( var i = 0 ; i < array.length ; i++ )
{
var record = array[i];
var alert = application.output(record.exception);
var alert = plugins.dialogs.showErrorDialog( "Error", "Error saving record. Please check all the fields.", 'OK');
databaseManager.rollbackEditedRecords()
}
}
}
So far it does what I was looking for.
Please let me know if there is anything that I missed to make this method work as efficient as possible.
Thanks you so much for the help.
Abrahim
Hi Abrahim,
Ask 20 people for their opinion on code efficiency and you may get 20 different opinions ![Laughing :lol:]()
However, I would suggest the following:
if ( examiner != null & wsc_date != null)
{
forms.beh_wisc_iii.controller.newRecord() ;
forms.beh_wisc_iii.subj_num = subj_num ;
forms.beh_wisc_iii.wsc_date = wsc_date ;
forms.beh_wisc_iii.examiner = examiner ;
forms.beh_wisc_iii.validated = validated ;
forms.beh_wisc_iii.wcs_coding_age = wcs_coding_age ;
forms.beh_wisc_iii.wcs_coding_raw = wcs_coding_raw ;
forms.beh_wisc_iii.wsc_age = wsc_age ;
forms.beh_wisc_iii.wsc_comp_age = wsc_comp_age ;
forms.beh_wisc_iii.wsc_which_study = wsc_which_study ;
databaseManager.saveData() ;
var array = databaseManager.getFailedRecords() ;
if (array[0] == null)
{
plugins.dialogs.showInfoDialog( "Hi", "Record was saved", 'Ok') ;
}
else
{
for ( var i = 0 ; i < array.length ; i++ )
{
var record = array[i];
var alert = application.output(record.exception);
plugins.dialogs.showErrorDialog( "Error", "Error saving record. Please check all the fields.", 'OK');
databaseManager.rollbackEditedRecords() ;
}
}
}
else
{
plugins.dialogs.showErrorDialog( "Data Missing", "Record not created. Please check all the fields.", 'OK');
}
I have altered a couple of things and removed things which I think were not necessary - but others may have further help to give
I am not an expert in arrays and so I was not sure that your original test of the the initialiser, (array == ‘’), would work so went for using the null value in element [0]
I have also added a default dialogue for the missing examiner and date data which is valuable feedback for the user - if it is needed.
Working on the assumption that there will only ever be 1 (one) unsaved record then you could get rid of the for () loop as there will be only array[0] to test and work with
Anyway, I hope that this helps
Cheers
Harry