AnsweredAssumed Answered

How to add unique name validation to accounts module

Question asked by Brett Zufelt on Mar 10, 2016
Latest reply on Mar 17, 2016 by Brett Zufelt

How do I add a custom validation to the Accounts module that prevents save if the name of the account is not unique?

I have been refereing to the Adding Field Validation to the Record View documentation from the Sugar developer guide and have succesfully added a validation function, I just don't know how to query the system to see if there already exists an account with the same name. Can anyone help?

 

I am on version 7.6 Enterprise.

 

Here is what I have so far. I created a new file in custom/modules/Accounts/clients/base/views/create-actions/create-actions.js with the following contents:

 

({
    extendsFrom: 'CreateActionsView',


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


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


    _doValidateUniqueName: function(fields, errors, callback) {        
        var accounts = app.data.createBeanCollection("Accounts");
       accounts.fetch({
          filter: [{'name':{'$equals':this.model.get('name')}}],
       });
        
        if (???) // How do I use the results of the fetch here?
        {
            errors['name'] = errors['name'] || {};
            errors['name'].required = true; // I plan on changing this to a custom error message
        }

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

 

The code is called correctly, I just don't know how to use the results of the accounts.fetch to determine if there are any accounts with the same name already.

Outcomes