How to throw and cach Logic Hook errors/exceptions and send error messages to log/GUI?

Hello!

I just wrote my frist Logic Hook, to clean and stardise the Accounts names (Company names) with a before save hook, like this:

<?php
    class Clean
    {
        function CleanName($bean, $event, $arguments)
        {
           $companyname = $bean->name;
           $companyname = ucwords(strtolower(trim(str_replace('"',' ',$companyname))));
           
           $bean->name = $companyname;
        }

    }
?>

Im curious what happens if there is an error in the variable and the logic hook cant run as expected? In this case i will never know where is the error if I cant get an error message from the logic hook.

How to throw error message to the screen , or if it simplier directly to the sugar log like this:

206.08.05. 09:17 - Error: "Custom Logic Hook function called CleanName casused error".

Asked the support, they said the to the log I can write like this:

$GLOBALS['log']->fatal("yourname: whatever");

But I dont know how to implement for this Logic Hook (never done before )

Im under Sugar 7.7.1. Enterprise.

Thank You for Your Help!

Regards,

Arpad

  • Hi Arnand, 


    Depends what you want to log, but you can use the log line above directly in the logic hook:

    <?php
        class Clean
        {
            function CleanName($bean, $event, $arguments)
            {
               $companyname = $bean->name;
               $companyname = ucwords(strtolower(trim(str_replace('"',' ',$companyname))));
               $bean->name = $companyname;
           
              //if(condition)
               $GLOBALS['log']->fatal("Some error occured. The $bean is: ".print_r($bean,true));

            }

        }
    ?>

    Is that what you meant? If not can you clarify your exact usecase?

  • In addition: If you want to validate the error before the logic hook, say for example that $companyname is not empty, I'd rather recommend to use a custom validation than checking this in the logic hook. 

    That way the user gets a notice before the record is saved that something with the data is not right. 

    Check out this example: 

    https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.7/User_Interface/Views/Adding_Field_V… 

  • Hello Dennis!

    Thanks for the help. Yes, would be better to user get the error message, so i start digging in the Field Validation tutorial. Simpler to prevent the error than handle it.

    Regards,

    Arpad