How to extend field validation to a field value of a related record

Hi,

I am trying to extend the Developer Guide example "Adding Field Validation to the Record View" method 1

http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.6/UI_Model/Views/Examples/Adding_Field…

I would like to work on the Contact record, but verify against a value from the related account.

For example, if the the user creates a new contact, and selects an account of type "internal", the UI should show an error and clear the account selection.

The above sample already gives me the framework, but I could not figure out how to access the type of the associated account.

How could this be done?

Best regards,

Holger

  • Within the code that gets executed for the validation, you could try something along these lines to get the info about the related account:

    var relAcctID = this.model.get('account_id');

    var relatedAccount = app.data.createBean('Accounts', {id:relAcctID});

    relatedAccount.fetch():

    var acctType = relatedAccount.get('account_type');

    if (acctType === 'Internal')

    {

    //the rest of your logic that tells Sugar it is valid/isn't valid.

    }

  • Hi Angel,

    Thank you! I tried your code, but it seems the account data is not yet available when the contact record is just being created.

    The variable acctType returns "undefined".

    Is there another way to do it?

    Thanks,

    Holger

  • I found out what was missing.

    The account record needs to be retrieved using a function call  to request the data.

    It looks like this:

    var contType = this.model.get('contact_type_c');

    var relAcctID = this.model.get('account_id');

    var accountBean = app.data.createBean('Accounts', {id:relAcctID});

    var request = accountBean.fetch();

    request.xhr.done(function () {

        var acctType = accountBean.get('account_type');

        // show console output

        console.log("ID of account: " + relAcctID);

        console.log("type of account: " + acctType);

        console.log("type of contact: " + contType);

        // more code for validation

    });

    The data retrieved by the call is only available inside the request.xhr.done(function () call, so I put the logic there.

    However, the variable contType is not available inside, even if I pass it by request.xhr.done(function (contType)

    Does anyone have an idea?

    Thanks,

    Holger

  • Are you certain that contType is set to begin with?  I tried similar code and it works fine for me.  Also, I think your code can be simplified by passing a success callback to fetch function instead of attaching callback to xhr request object.  The danger there is you could override Backbone or Sidecar contributed callbacks.

    http://backbonejs.org/#Collection-fetch

    You can pass the fetch() call an options hash with success and/or error callbacks.

    var accountBean = SUGAR.App.data.createBean('Accounts', {id:'...GUID...'});
    var test = "test.";
    accountBean.fetch({'success':function () {
        var acctType = accountBean.get('account_type');
        console.log(test);
        console.log(acctType);
    }});
    

    App Ecosystem @ SugarCRM