Autoincrement number on Contracts in Suitecrm8

Hi,

I’m trying to add autoincrement number in Contracts for Suitecrm8. I’ve added after save hook:

<?php

if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

function setContractNumber(&$bean, $event, $arguments) {
    if (empty($bean->contract_number_c)) {
        try {
            $query = "SELECT MAX(contract_number_c) AS max_number FROM aos_contracts_cstm";
            $result = $bean->db->query($query);
            $row = $bean->db->fetchByAssoc($result);

            $max_number = $row['max_number'] ?? 0; // Handle null values
            $bean->contract_number_c = $max_number + 1;
        } catch (Exception $e) {
            sugar_die('Error setting contract number: ' . $e->getMessage());
        }
    }
}
?>

But it returns blank screen… Maybe someone has any ideas?

Can you change a setting in php.ini for display_errors=On temporary to get error details.
OR
You can change config_override.php to set logger level as $sugar_config['logger']['level'] = 'debug'; and check your logs file (public/legacy/suitecrm.log) to debug the issue.

If you want to save/modify data in a record form a logic hook you should use a before_save hook instead of an after_save hook.

Sry, I’ve tryed before save hook.

Logger shows only:


Mon Dec 16 12:07:29 2024 [3349256][1][DEBUG] Hook called: AOS_Contracts::before_save
Mon Dec 16 12:07:29 2024 [3349256][1][DEBUG] Including Ext hook file for custom/modules/AOS_Contracts
Mon Dec 16 12:07:29 2024 [3349256][1][DEBUG] Creating new instance of hook class AOS_Contracts without parameters
Mon Dec 16 12:07:29 2024 [3349256][1][DEBUG] Hook called: ::server_round_trip
Mon Dec 16 12:07:29 2024 [3349256][1][DEBUG] Calling MySQLi::disconnect()

And After nothing happens…

If the code you shared is the complete file you also have to take in count that the function should be contained within a class and then the class has to be specified on the logic_hooks.php file. You can look examples here: Logic Hooks :: SuiteCRM Documentation

Added clas, but nothing changes… Maybe any new ideas?

Hook call:

<?php
$hook_version = 1;
$hook_array = array();
$hook_array['before_save'] = array();
$hook_array['before_save'][] = array(1, 'Set Contract Number', 'custom/modules/AOS_Contracts/contract_number_logic.php', 'AOS_Contracts', 'setContractNumber');
?>

Hook:

<?php

if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class AOS_Contracts
{
function setContractNumber(&$bean, $event, $arguments) {
    if (empty($bean->contract_number_c)) {
        try {
            $query = "SELECT MAX(contract_number_c) AS max_number FROM aos_contracts_cstm";
            $result = $bean->db->query($query);
            $row = $bean->db->fetchByAssoc($result);

            $max_number = $row['max_number'] ?? 0; // Handle null values
            $bean->contract_number_c = $max_number + 1;
        } catch (Exception $e) {
            sugar_die('Error setting contract number: ' . $e->getMessage());
        }
    }
}
}
?>

And i have blank screen, and contract not saving at all.

you should not use the class name AOS_Contracts because it is the default name of the Contracts bean class. please use something like ContractsLogickHooks