Avoid duplicate entries in the suitecrm

I want to avoid duplicate entry in the db and tell the user that data already exisits in the form before save …
Please tell how to do it because i’m new to the suitecrm Thanks in advance

You could write a custom “before_save” hook to check the data is unique before it will continue and save. If it’s not unique, you can exit without saving, with an error code.

I have tried But it didn’t workout…That’s why I am asking for help

It would help if you would post the part of your code that is supposed to check if the data is duplicate and if it is, avoid the save and show a message.

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

class PreventDuplicateEntry
    function checkDuplicateEntry($bean, $event, $arguments)
        try {
            error_log("Logic Hook Triggered: before_save");

            $numbers = $GLOBALS['db']->quote($bean->numbers_c);
            $beanId = $GLOBALS['db']->quote($bean->id_c);

            $query = "SELECT COUNT(*) AS count FROM pat_patients_cstm WHERE numbers_c = {$numbers} AND id_c != {$beanId}";
            $result = $GLOBALS['db']->query($query);
            $row = $GLOBALS['db']->fetchByAssoc($result);

            if ($row['count'] > 0) {
                throw new Exception('Duplicate entry found.');
        } catch (Exception $e) {
            sugar_die('Error: ' . $e->getMessage());

this is my customlogic code
Developer Collaboration suitecrm7

When you run the SQL from the database command prompt, does your duplicate logic work as you expect?
I’m wondering what’s the reason you’re including the bean ID, as those should be auto generated and always unique for attempted new beans, which you’re not sure yet if this bean should be created as a new bean or is actually a duplicate of an existing one, thru the numbers field, is that your intention?
And why is it a custom bean ID by itself, isn’t it supposed to be joined thru SQL on the main table bean ID? To keep them matched up with the same bean ID? So that both tables deleted field can be set to 1 when it’s found out that it’s a duplicate?

Maybe helpful add-on:

Duplicate Cleaner | SuiteCRM Module.