How to Make validation duplication entry  for custom module ?

i want to make validation for duplicate Entry ,

For Example- I have make from Studio  Custom Module [Program ]And I want to validate to duplicate entry in data base  

Parents
  • hi

    i Am  unable to do that ...

    i want to do Stop duplicate entry to the database ..

    when entered Duplicate name display message duplicate record.

    i have a custom module that has fields name and Description,and contact name and director name

  • Hi MK Gupta,

    I want to share with you one solution which works if you just want to avoid duplicate entries for a given module without using the duplicate check feature of SugarCRM. Unlike the duplicate check default feature of Sugar,this solution does not let you to duplicate the record (it just triggers an error telling you that the record is duplicated).

    This solution is for the Accounts module and what it is checking if the name of the account is equal as the new account, so if they are the error will be shown to the user.

    Here is the definition of the LogicHook located at custom/Extension/modules/Accounts/Ext/LogicHooks/validate_duplicate_record_lh_definition.php:

    $hook_array['before_save'][] = Array(
        //Processing index. For sorting the array.
        1,

        //Label. A string value to identify the hook.
        'validate_duplicate_record',

        //The PHP file where your class is located.
        'custom/modules/Accounts/scripts/validate_duplicate_record_lh.php',

        //The class the method is in.
        'ValidateDuplicateRecord',

        //The method to call.
        'validateDuplicateRecord'
    );

    Here is the code of the LogicHook:

    require_once 'include/api/SugarApiException.php';
    require_once 'include/SugarQuery/SugarQuery.php';

    class ValidateDuplicateRecord {

        //Checks if the account name already exists
        function validateDuplicateRecord( $bean, $event, $arguments ) {

            if( !$bean ) {
                return;
            }

            $accountId = $bean->id;
            $accountName = $bean->name;

            $duplicateAccountMessage = 'Error: An account with name "' . $accountName . '" already exists';

            $accountBean = BeanFactory::getBean( 'Accounts' );

            $sq = new SugarQuery();

            $sq->from( $accountBean, array('team_security' => false) );
            $res = $sq->execute();

            foreach( $res as $index=>$account ) {
                if( $account['id'] !== $accountId && $account['name'] === $accountName ) {
                    throw new SugarApiExceptionInvalidParameter($duplicateAccountMessage);
                }
            }
        }
    }

    Hope this helps,

    David.

Reply Children
No Data