Custom Duplicate Checking

Hi, I’m new to Suite CRM and want to add a duplicate check for the fields to check in the database if the value exists. Can anyone help me on this task?

@benjiemoon

You can:

  1. write js code which will request a database.
  2. create “entrypoint”.
    Look at the documentation for an example.
    Metadata :: SuiteCRM Documentation
    Entry Points :: SuiteCRM Documentation

Not working. Maybe its my codes can you help check.
Here I’m Just checking the province for duplicates.

EntryPointRegistry:

EntryPoint:

@benjiemoon

  1. Did you do “Quick Repair and Rebuild”?
  2. Which error are you getting?

Yeah I did. no error just did not work… BTW this is a custom module

@p.konetskiy , I have same error sir. the Validation is working but after the validation the data will not save nor update.

Check my custom view.edit.php codes below:

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

class AccountsViewEdit extends ViewEdit
{
    public function display()
    {
        if(!empty($this->bean->mobile_number))
        {
            $this->bean->mobile_number = '0'.substr($this->bean->mobile_number, 3);
        }
        parent::display(); // TODO: Change the autogenerated stub

        $javascript = <<<'EOT'
        <script type= "text/javascript">
        function customJavascriptDuplicateValidation(thisview)
        {
            var firstname = document.getElementById('first_name').value;
            var lastname = document.getElementById('last_name').value;
            var mobile = document.getElementById('mobile_number').value;
            var birthday = document.getElementById('birthday').value;
            var email = document.getElementById('email_c').value;
            var location = document.getElementById('location_code').value;
            var consent = document.getElementById('consent_timestamp_date').value;
            var salutation = document.getElementById('salutation').value;
           

            $.post('index.php?entryPoint=checkDuplicateCustomers', { first_name: firstname, last_name: lastname, mobile_number: mobile }, function(data)
            {
                if (data == 'exists') {
                    $('#error_firstname_msg').html('<strong style="color:red;"> &#x2717; Already Used</strong>');
                    $('#error_lastname_msg').html('<strong style="color:red;"> &#x2717; Already Used</strong>');
                    $('#error_mobile_msg').html('<strong style="color:red;"> &#x2717; Already Used</strong>');
                    return check_form('EditView');
                    return false;
                }else if (data == 'unique'){
                    $('#error_firstname_msg').html('<strong style="color:green;"> &#x2713;</strong>');
                    $('#error_lastname_msg').html('<strong style="color:green;"> &#x2713;</strong>');
                    $('#error_mobile_msg').html('<strong style="color:green;"> &#x2713;</strong>');
                    if(firstname != '' && lastname != '' && mobile != '' && birthday != '' && email != '' && location != '' && consent != '' && salutation != ''){
                        //SUGAR.ajaxUI.showLoadingPanel();
                        return check_form('EditView');
                        return true;
                    }else{
                        return check_form('EditView');
                        return false;
                    }
                }else{
                    $('#error_firstname_msg').hidden;
                    $('#error_lastname_msg').hidden;
                    $('#error_mobile_msg').hidden;
                    return check_form('EditView');
                    return false;
                }
            });
        }
        </script>
        EOT;

        echo $javascript;

    }
}

And my custom save buttons at editviewdefs.php

$viewdefs['Accounts']['EditView']['templateMeta']['form']['buttons'][0] = array(
    'customCode' => '<input title="Save" accesskey="a" class="button primary" onclick="var _form = document.getElementById(\'EditView\'); _form.action.value=\'Save\'; if(customJavascriptDuplicateValidation(\'EditView\'))SUGAR.ajaxUI.submitForm(_form);return false;" type="submit" name="button" value="Save" id="SAVE">',
);

Result:


After clicking save the native validation will validate first if all required field are not empty.


Once you input data on First, Last Name and Mobile Number my custom validation will check the database if those three criteria is already exist. The custom validation will work only if any of the criteria field are not empty.


After the validation if the data is not duplicate the check validation will appear to tell the users that the account information is unique.

Note: We allow the users to input the same First name and Last name as long as the mobile number is not the same.


This is my problem, after the validation passed the data won`t save or update. Nothing’s happened.

@waraikobuot
There is some very strange code in your js. Will work the first “return” only.

@benjiemoon
You didn’t answer to the second question:

Good Day!

@p.konetskiy , Sorry sir I forgot to remove the other return. But I was able to fix it and its working now. Here`s my code.

$javascript = <<<'EOT'
        <script type="text/javascript" language="JavaScript">
        function customJavascriptDuplicateValidation(thisview)
        {
            var firstname = document.getElementById('first_name').value;
            var lastname = document.getElementById('last_name').value;
            var mobile = document.getElementById('mobile_number').value;
            var birthday = document.getElementById('birthday').value;
            var email = document.getElementById('email_c').value;
            var location = document.getElementById('location_code').value;
            var consent = document.getElementById('consent_timestamp_date').value;
            var salutation = document.getElementById('salutation').value;
            var gender = document.getElementById('gender').value;
            var data_id = document.getElementById('id').value;
            var mobileformat = /^(\+639)\d{9}$/;
            
            $.get('index.php?entryPoint=checkDuplicateCustomers', { first_name: firstname, last_name: lastname, mobile_number: mobile, data_id: data_id }, function(data)
            {
                if (data == 'exists') {
                    $('#error_firstname_msg').html('<i style="color:red;"> &#x2717; Already Used</i>');
                    $('#error_lastname_msg').html('<i style="color:red;"> &#x2717; Already Used</i>');
                    $('#error_mobile_msg').html('<i style="color:red;"> &#x2717; Already Used</i>');
                    return check_form('EditView'); 
                }else if (data == 'unique'){
                    $('#error_firstname_msg').html('<i style="color:green;"> &#x2713;</i>');
                    $('#error_lastname_msg').html('<i style="color:green;"> &#x2713;</i>');
                    $('#error_mobile_msg').html('<i style="color:green;"> &#x2713;</i>');
                    saveData();
                    
                }else if(data == 'unmodified'){
                    saveData();
                }
                $('#error_firstname_msg').hidden;
                $('#error_lastname_msg').hidden;
                $('#error_mobile_msg').hidden;
                return check_form('EditView'); 
            });
            
            function saveData(){
                if(firstname != '' && lastname != '' && mobile != '' && gender !='' && birthday != '' && email != '' && location != '' && consent != '' && salutation != ''){
                    if(!mobile.match(mobileformat))
                    {
                       return check_form('EditView'); 
                    }
                    SUGAR.ajaxUI.showLoadingPanel();
                    var _form = document.getElementById('EditView');
                        _form.action.value='Save';
                    SUGAR.ajaxUI.submitForm(_form);
                    return check_form('EditView'); 
                }
                return check_form('EditView'); 
            }
            
        }
        </script>
        EOT;

I just added this line below, I dont know the reason why the **check_form('EditView')** will only validate but wont save data if it is inside of the $.GET function.

SUGAR.ajaxUI.showLoadingPanel();
                    var _form = document.getElementById('EditView');
                        _form.action.value='Save';
                    SUGAR.ajaxUI.submitForm(_form);
                    return check_form('EditView'); 
1 Like

@waraikobuot
I think you problem is here.

Hi @p.konetskiy, right now sir everything is ok and working fine. But I have a new issue right now. I don`t know if what I did is right or not.

I added this function below on my javascript in view.edit.ph

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

class dvt0_LocationViewEdit extends ViewEdit
{
    public function display()
    {

        $javascript = <<<'EOT'
        <script type="text/javascript" language="JavaScript">
        function customJavascriptDuplicateValidation(thisview)
        {
            var province = document.getElementById('custom_province_c').value;
            var city = document.getElementById('custom_city_c').value;
            var barangay = document.getElementById('custom_barangay_c').value;
            var data_id = document.getElementById('id').value;
            var message = 'Ops! Already Exist';
            $.get('index.php?entryPoint=checkDuplicateLocations', { custom_province_c: province, custom_city_c: city, custom_barangay_c: barangay, data_id: data_id }, function(data)
            {
                if (data == 'exists') {
                    setFieldErrorMessage('custom_province_c',message);
                    setFieldErrorMessage('custom_city_c',message);
                    setFieldErrorMessage('custom_barangay_c',message);
                    return check_form('EditView'); 
                }else if (data == 'unique'){
                    saveData();
                    
                }else if(data == 'unmodified'){
                    saveData();
                }
                return check_form('EditView'); 
            });
            
            function saveData(){
                if(province != '' && city != '' && barangay != ''){
                    SUGAR.ajaxUI.showLoadingPanel();
                    var _form = document.getElementById('EditView');
                        _form.action.value='Save';
                    SUGAR.ajaxUI.submitForm(_form);
                    return check_form('EditView'); 
                }
                return check_form('EditView'); 
            }
            
            function setFieldErrorMessage(field, message){
                hideFieldErrorMessage(field);
                $('#' + field).css('background-color', 'red');
                $('#' + field).animate({'background-color': 'white'}, 1500);
                $('#' + field).after('<div class="required validation-message">'+message+'</div>');
            }
            
            function  hideFieldErrorMessage(field) {
                console.log(field);
                $('#' + field).css('background-color', 'initial');
                var errorElem = $('#' + field).next();
                console.log(errorElem);
                if(errorElem.hasClass('required') && errorElem.hasClass('validation-message')) {
                    errorElem.remove();
                }
            }
            
            
        }
        </script>
        EOT;

        echo $javascript;
        parent::display(); // TODO: Change the autogenerated stub

    }
}

the validation is ok, but when I remove the value of the dropdown the custom error message will not disappear. Please see below screenshot for your reference.

I have a doubt on this …