Simple logic hook Error 500 on On-Demand

I created a namespaced before_save logic hook that queries the DB for the largest number in a custom field, increments the result and sets that on the record when a new opportunity is created.

It works perfectly on On-Premise, but responds with an Error 500 on On-Demand instances without creating the record and with no output in the sugarcrm.log.

Here is the class file with the method that is called:

<?php

namespace Sugarcrm\Sugarcrm\custom\modules\Opportunities;

class IncrementalNumber
{
    private const MODULE = 'Opportunities';
    private const FIELD = 'opp_no_c';
    private const STARTING_NUMBER = 10000;

    /**
     * Retrieve the largest number for the specified field from the database,
     * increment it and set it in the new record before it is saved.
     * If no number was found in the database, set it to the defined starting number.
     */

    function incrementNumber($bean, $event, $arguments)
    {
        if ($arguments['isUpdate']) return;  // Only new records.
        if (!empty($bean->{self::FIELD})) return;  // If number not already set.

        $table = strtolower(self::MODULE) . '_cstm';
        $field = self::FIELD;

        $query = "SELECT MAX({$field}) FROM {$table}";  // Largest number in the column.
        $lastNumber = $GLOBALS['db']->getOne($query);  // Fetch just one result.
       
        if ($lastNumber)
        {
            // Increment the largest number and set it in the current record.
            $bean->$field = ++$lastNumber;
        }
        else
        {
            // Set the number in the current record to the defined starting number.
            $bean->$field = self::STARTING_NUMBER;
        }
    }
}