1

I have a custom module where there is an email field. Now i want to stop the user if the email is already in the database.

I want to stop the user on save button and show the error. Like when a required field goes empty.

I tried to get some help but was not able to understand it.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
programmer
  • 115
  • 1
  • 2
  • 12
  • Have you checked this https://stackoverflow.com/questions/23340660/prevent-duplicate-value-using-ajax-in-sugar-crm – mrbarletta Oct 29 '19 at 13:51
  • @mrbarletta thank you for your reply . actually i want to call ajax on save button not on focusout on the email field as it can be bypassed and user can still click on save and save the record. – programmer Oct 29 '19 at 14:12
  • what about this answer then https://stackoverflow.com/questions/55141324/custom-validation-for-unique-custom-field/55155159#55155159 – mrbarletta Oct 29 '19 at 21:55

1 Answers1

0

Note: I realized after posting this that you are using suitecrm which this answer will not be applicable toward but I will leave it in case anyone using Sugar has this question.

There are a couple of ways to accomplish this so I'll do my best to walk through them in the order I would recommend. This would apply if you are using a version of Sugar post 7.0.0.

1) The first route is to manually create an email address relationship. This approach would use the out of box features which will ensure your system only keeps track of a single email address. If that would work for your needs, you can review this cookbook article and let me know if you have any questions:

https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_9.2/Cookbook/Adding_the_Email_Field_to_a_Bean/

2) The second approach, where you are using a custom field, is to use field validation. Documentation on field validation can be found here:

https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_9.2/Cookbook/Adding_Field_Validation_to_the_Record_View/index.html

The code example I would focus on is:

https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_9.2/Cookbook/Adding_Field_Validation_to_the_Record_View/#Method_1_Extending_the_RecordView_and_CreateView_Controllers

For your example, I would imagine you would do something like this:

Create a language key for your error message:

./custom/Extension/application/Ext/Language/en_us.error_email_exists_message.php

<?php

$app_strings['ERROR_EMAIL_EXISTS_MESSAGE'] = 'This email already exists.';

Create a custom controller for the record creation (you may also want to do this in your record.js):

./custom/modules//clients/base/views/create/create.js

({
    extendsFrom: 'RecordView', 

    initialize: function (options) {

        this._super('initialize', [options]);

        //reference your language key here
        app.error.errorName2Keys['email_exists'] = 'ERROR_EMAIL_EXISTS_MESSAGE';

        //add validation tasks
        this.model.addValidationTask('check_email', _.bind(this._doValidateEmail, this));
    },

    _doValidateEmail: function(fields, errors, callback) {

        var emailAddress = this.model.get('your_email_field');

        //this may take some time so lets give the user an alert message
        app.alert.show('email-check', {
            level: 'process',
            title: 'Checking for existing email address...'
        });

        //make an api call to a custom (or stock) endpoint of your choosing to see if the email exists
        app.api.call('read', app.api.buildURL("your_custom_endpoint/"+emailAddress), {}, {
            success: _.bind(function (response) {
                //dismiss the alert
                app.alert.dismiss('email-check');

                //analyze your response here
                if (response == '<email exists>') {
                    errors['your_email_field'] = errors['your_email_field'] || {};
                    errors['your_email_field'].email_exists = true;
                }

                callback(null, fields, errors);
            }, this),
            error: _.bind(function (response) {
                //dismiss the alert
                app.alert.dismiss('email-check');

                //throw an error alert
                app.alert.show('email-check-error', {
                    level: 'error',
                    messages: "There was an error!",
                    autoClose: false
                });

                callback(null, fields, errors);
            })
        });
    },
})

Obviously, this isn't a fully working example but it should get you most of the way there. Hope this helps!