Hi,
I'm afraid I don't know if this is a bug introduced in 5.2.9, or if it was there before, as I've only just been trying to do this today after updating last night.
The bug seems to be that the parameters passed into a function are being changed indirectly by changing local variables that have been assigned to them
I am working on an appointments system as part of a solution and am using the IT2Be Calendar bean.
My client wants to be able to block book a day, week or month's worth of time slots in one go for the logged in user.
There is a button for each day/week/month creation which basically loop thru an array of the days & times and at the lowest level call another method that when given a start & end datetime create an event record with various set values.
I am also using the Dr Maison dateutils plugin that generates a date series for me in an array, so I can get the user to pick a start date & time and an end date & time with a duration (span) and the dateutils creates an array with all the different time slots in it. However, maybe I'm not doing it correctly or something, it is not generating the final array value for the last time slots end time.
So my lower function that creates the event is looking for a 'null' end datetime and I am then saying - set the enddatetime to the startdatetime + the duration/span, BUT as well as setting the enddatetime it is also setting the startdatetime parameter, even though I am NOT directly assigning the startdatetime!!
Here is my code and console output for a few appts on one day
- Code: Select all
/**
* @param {Date} startdate Start date
*
* @param {Date} enddate End date
*
* @properties={typeid:24,uuid:"A422ADC4-9371-4BC7-8BDD-4AFEC6DA89E5"}
*/
function add_event_in_loop ( startdate, enddate )
{
var $start_date = startdate ;
var $end_date = enddate ;
application.output ( 'inner function call before doing anything. Startdate param = ' + startdate + ' local var = ' + $start_date + ' enddate param = ' + enddate + ' local var = ' + $end_date );
//Stop executing when the start_date is empty
if ( !startdate )
{
application.output ( "Halting here...", LOGGINGLEVEL.INFO );
return;
}
// check boundary condition, as dateutils doesn't set last element for me
if ( $end_date == null )
{
$end_date = $start_date;
var $mins = $end_date.getMinutes ( ) ;
$end_date = $end_date.setMinutes ( ( $mins + globals.span_minutes) ); // !!!!!THIS IS WHERE IT GOES WRONG!!!!!!
application.output ( 'after null assign. Startdate param = ' + startdate + ' local var = ' + $start_date + ' enddate param = ' + enddate + ' local var = ' + $end_date );
}
//Add the record to the form and fill the date fields
globals.mod_pos_temp_start_date = $start_date ;
globals.mod_pos_temp_end_date = $end_date ;
if ( databaseManager.hasRecords ( g_check_event ) )
{
// event already exists, should we do something?
// application.output ( 'Event already there...' );
}
else
{
application.output ( 'about to set record values. Startdate param = ' + startdate + ' local var = ' + $start_date + ' enddate param = ' + enddate + ' local var = ' + $end_date );
var $record = foundset.getRecord(foundset.newRecord());
$record.startdatetime = $start_date;
$record.enddatetime = $end_date;
$record.id_staff = globals.user_ID;
$record.team_code = $record.events_to_staff.team_code;
$record.realcategoriesid = $record.events_to_staff.realcategoriesid;
$record.flag_available = globals.flag_available;
$record.flag_ok_for_1st_appt = globals.flag_ok_1st;
if ( globals.mod_pos_colour != null )
{
$record.colour = globals.mod_pos_colour;
}
$record.description = globals.mod_pos_text;
databaseManager.saveData($record);
}
}
/**
*
* @properties={showInMenu:true,typeid:24,uuid:"C513357C-4D94-457C-AE0C-F3F27F5EDC7C"}
*/
function add_events_day_loop ()
{
globals.mod_pos_pleaseWait ( 'Creating Days Appointments...' );
// Create start and end Date for the series
globals.mod_pos_start_date = elements.it2be_calendar.date;
globals.mod_pos_end_date = elements.it2be_calendar.date;
globals.mod_pos_start_date = globals.mod_pos_start_date.setHours ( 8 );
globals.mod_pos_start_date = globals.mod_pos_start_date.setMinutes ( 30 );
globals.mod_pos_start_date = globals.mod_pos_start_date.setSeconds ( 0 );
globals.mod_pos_start_date = globals.mod_pos_start_date.setMilliseconds ( 0 );
globals.mod_pos_end_date = globals.mod_pos_end_date.setHours ( 17 );
globals.mod_pos_end_date = globals.mod_pos_end_date.setMinutes ( 30 );
globals.mod_pos_end_date = globals.mod_pos_end_date.setSeconds ( 0 );
globals.mod_pos_end_date = globals.mod_pos_end_date.setMilliseconds ( 0 );
forms.et_FID_appt_create.time_span_text = 'Day';
forms.et_FID_appt_create.time_span_no = '1';
application.showFormInDialog ( forms.et_FID_appt_create, -1, -1, -1, -1, 'Create Slots', false, false, false );
if ( globals.mod_pos_fid_ok == 1 )
{
var vPeriod = plugins.DateUtils.Period ( 0, 0, 0, globals.appt_hours, globals.appt_minutes );
globals.span_minutes = ( globals.appt_hours > 0 ) ? ( ( globals.appt_hours * 60 ) + globals.appt_minutes ) : globals.appt_minutes;
globals.mod_pos_start_date = globals.mod_pos_start_date.setMinutes ( globals.mod_pos_start_date.getMinutes ( ) - globals.span_minutes );
globals.mod_pos_end_date = globals.mod_pos_end_date.setMinutes ( globals.mod_pos_end_date.getMinutes ( ) - globals.span_minutes );
// Create the date series
var vDates = plugins.DateUtils.createDateSeries ( globals.mod_pos_start_date, vPeriod, globals.mod_pos_end_date );
var lv_len = vDates.length;
for ( var i = 0; i < lv_len; i++ )
{
application.output('loop ' + i);
application.output ( 'before inner loop call ' + vDates[i] + ' ' + vDates[i + 1] );
add_event_in_loop ( vDates[i], vDates[i + 1] );
}
databaseManager.saveData ( );
refresh_form ( );
}
globals.mod_pos_ready ( );
}
so the lower method calls the one above it...
here is the output from the console
loop 0
before inner loop call Mon Jul 04 16:00:00 BST 2011 Mon Jul 04 16:30:00 BST 2011
inner function call before doing anything. Startdate param = Mon Jul 04 16:00:00 BST 2011 local var = Mon Jul 04 16:00:00 BST 2011 enddate param = Mon Jul 04 16:30:00 BST 2011 local var = Mon Jul 04 16:30:00 BST 2011
about to set record values. Startdate param = Mon Jul 04 16:00:00 BST 2011 local var = Mon Jul 04 16:00:00 BST 2011 enddate param = Mon Jul 04 16:30:00 BST 2011 local var = Mon Jul 04 16:30:00 BST 2011
loop 1
before inner loop call Mon Jul 04 16:30:00 BST 2011 Mon Jul 04 17:00:00 BST 2011
inner function call before doing anything. Startdate param = Mon Jul 04 16:30:00 BST 2011 local var = Mon Jul 04 16:30:00 BST 2011 enddate param = Mon Jul 04 17:00:00 BST 2011 local var = Mon Jul 04 17:00:00 BST 2011
about to set record values. Startdate param = Mon Jul 04 16:30:00 BST 2011 local var = Mon Jul 04 16:30:00 BST 2011 enddate param = Mon Jul 04 17:00:00 BST 2011 local var = Mon Jul 04 17:00:00 BST 2011
loop 2
before inner loop call Mon Jul 04 17:00:00 BST 2011 undefined
inner function call before doing anything. Startdate param = Mon Jul 04 17:00:00 BST 2011 local var = Mon Jul 04 17:00:00 BST 2011 enddate param = undefined local var = undefined
after null assign. Startdate param = Mon Jul 04 17:30:00 BST 2011 local var = Mon Jul 04 17:30:00 BST 2011 enddate param = undefined local var = 1309797000000
about to set record values. Startdate param = Mon Jul 04 17:30:00 BST 2011 local var = Mon Jul 04 17:30:00 BST 2011 enddate param = undefined local var = 1309797000000
I might be doing something wrong, but I don't think so. It looks like Servoy is using pointers or something to reference the variables, thereby linking them to the parameters they were assigned to (i.e. the same memory slot) instead of allocating separate ones. (BTW, before using the local variable, I was just using the passed parameters and changing the passed 'endtime' param and changing it, but this also changed the 'starttime', so I then tried using local variables)
Thanks
Rafi