diff --git a/classes/sdapi.php b/classes/sdapi.php
index f3688e0f2f49de365f54c53c7a3b5dbaa2267a0b..64e6f8a6efeb61389b44fe93190602393c134fce 100644
--- a/classes/sdapi.php
+++ b/classes/sdapi.php
@@ -1288,7 +1288,11 @@ class sdapi {
     }
 
     private function update_course_appointments($trace, $updatedappointments) {
-        global $DB;
+        global $CFG, $DB;
+        if (!$updatedappointments) {
+            return 0;
+        }
+        require_once($CFG->dirroot . '/calendar/lib.php');
 
         $trace->output('Starting course appointment update...');
 
@@ -1339,7 +1343,11 @@ class sdapi {
     }
 
     private function update_group_appointments($trace, $updatedappointments) {
-        global $DB;
+        global $CFG, $DB;
+        if (!$updatedappointments) {
+            return 0;
+        }
+        require_once($CFG->dirroot . '/calendar/lib.php');
 
         $trace->output('Starting group appointment update...');