Custom error messages suiteCRM 8.7.1

Afternoon
i have a custom module which has a field called admission that has to be unique everytime so i and AI did a before_save logic hook that checks the database if we have a similar entry and blocks the save. so far so good the save is blocked if we have a similar entry but i can get to display a custom message to make users aware of the duplicate. How do i get to display this custom message.

here is my working hook

<?php

class AdmissionCheckHook
{
    public function preventDuplicateAdmission(&$bean, $event, $arguments)
    {
        global $db, $log;

        $admission = trim($bean->admission ?? '');
        if (empty($admission)) {
            $log->fatal("❌ No admission provided.");
            return;
        }

        $escapedAdmission = strtolower(addslashes($admission));
        $query = "
            SELECT id 
            FROM dp_diaryoperations 
            WHERE deleted = 0 
              AND LOWER(admission) = '{$escapedAdmission}'
        ";

        if (!empty($bean->id)) {
            $escapedId = addslashes($bean->id);
            $query .= " AND id != '{$escapedId}'";
        }

        $log->fatal("🔍 Checking admission uniqueness: '{$admission}'");

        try {
            $result = $db->query($query);
            $row = $db->fetchByAssoc($result);

            if (!empty($row['id'])) {
                $msg = "🚫 Admission '{$admission}' already exists — save blocked.";
                $log->fatal("â›” {$msg}");

                throw new Exception($msg);
            	throw new SugarFatalErrorException(translate('LBL_ADMISSIONDUPLICATE_ERROR', $bean->DP_DiaryOperations));
            }

            $log->fatal("✅ Admission is unique — save allowed.");
        } catch (Exception $e) {
            $log->fatal("❌ DB Error or exception: " . $e->getMessage());
            throw $e;
        }
    }
}

@Zed To properly display a custom error message in SuiteCRM when a duplicate admission entry is detected, you should avoid using a generic Exception or SugarFatalErrorException, as these won’t show user-friendly messages in the UI. Instead, you should use SugarApiExceptionInvalidParameter, which is specifically handled by SuiteCRM to display errors in a readable format to end users. In your logic hook, once you’ve identified that the admission value already exists in the database, you can throw this exception with your custom message. For example, after detecting a duplicate, use: throw new SugarApiExceptionInvalidParameter("Admission '{$admission}' already exists. Please enter a unique admission."); This will display the message directly on the screen in a red error alert. Optionally, you can move the message to a language file by adding it to custom/Extension/modules/YourModule/Ext/Language/en_us.custom.php and retrieving it using the translate() function. This approach ensures a clean and user-friendly experience

@Urvi Thanks for the clear and well put response for some reason i keep getting the below messages
Error occurred while saving recordĂ—

You are not authorized to view this page. Please contact your system administrator.
is it possible that maybe they are supressing my custom message

@Zed
The error message “You are not authorized to view this page” in SuiteCRM 8 typically means that your custom exception is not being handled correctly by the system, and instead, it’s falling back to a default access-denied response. This can happen when a generic Exception or SugarFatalErrorException is thrown from a logic hook, especially during sidecar-based form submissions where the context expects API-friendly errors. To properly display a custom validation message to the user, you should use SugarApiExceptionInvalidParameter, which is specifically designed to show error messages in the SuiteCRM UI. However, in SuiteCRM 8, especially within logic hooks, this class might not be autoloaded, so it’s important to include it manually using require_once 'include/api/SugarApiExceptionInvalidParameter.php'; before throwing it. Once you detect a duplicate admission, you should throw a single, clean exception like: throw new SugarApiExceptionInvalidParameter("Admission '{$admission}' already exists. Please enter a unique admission."); Additionally, to keep things translatable and clean, you can define the message in a language file (e.g., en_us.custom.php) and use translate('LBL_ADMISSIONDUPLICATE_ERROR', 'YourModule') instead. Make sure to clear SuiteCRM’s cache and run Quick Repair and Rebuild after making these changes. Also, confirm that the user has proper access permissions to the module, as that can also trigger this unauthorized error. Following these steps will ensure your custom error message is shown properly in the UI without being suppressed.

I doubt that you can throw API exceptions outside the context of API requests, and I doubt that inside a logic hook there is such a context…

Logic hooks are back-end, and they are legacy, and they are not the place to try and send messages to the front-end.

I would suggest looking into v8-specific front-end ways of validating things for users.

Maybe this is helpful:

Please check if this helps