diff --git a/src/ActionsGridFieldItemRequest.php b/src/ActionsGridFieldItemRequest.php
index e53f4b0..e64a2f1 100644
--- a/src/ActionsGridFieldItemRequest.php
+++ b/src/ActionsGridFieldItemRequest.php
@@ -798,12 +798,13 @@ public function doSaveAndNext($data, $form)
         $controller = $this->getToplevelController();
         $controller->getResponse()->addHeader("X-Pjax", "Content");
 
-        $class = get_class($record);
-        $getNextRecordID = $this->getCustomNextRecordID($record);
         $class = get_class($record);
         if (!$class) {
             throw new Exception("Could not get class");
         }
+
+        $getNextRecordID = $this->getCustomNextRecordID($record);
+
         /** @var ?DataObject $next */
         $next = $class::get()->byID($getNextRecordID);
 
@@ -837,8 +838,11 @@ public function doSaveAndPrev($data, $form)
         $controller->getResponse()->addHeader("X-Pjax", "Content");
 
         $class = get_class($record);
+        if (!$class) {
+            throw new Exception("Could not get class");
+        }
+
         $getPreviousRecordID = $this->getCustomPreviousRecordID($record);
-        $class = get_class($record);
         if (!$class) {
             throw new Exception("Could not get class");
         }