Hi,
I think it miss this command before triggering.
$event->add_record_snapshot($tablename, $record);
can be
$event->add_record_snapshot('course', $course);
If you look in Moodle code for add_record_snapshot you'll find another usefull sample
$event->add_record_snapshot('user', $user);
$event->add_record_snapshot('course_modules', $cm);
...
Hope it's help.
Dominique.