Custom field in account custom name view not available in afterRender

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    Custom field in account custom name view not available in afterRender

    I have create a custom view for the name field on the account, i have a custom boolean field isActive. the purpose if to show a little icon next to the name based on the state of the account (active or incative), the name always come with the default value as inactive when i double checked i found that custom fields on account are always undefined in first initialisation, i have to visit the account and go back to list to get the correct result, below my code: i have logged the result of the id - name and my custom field isActive and seems that id and name are loaded on first initialisation but my custom field isActive is undefined.

    PHP Code:
    define('spotlight:views/account/fields/name',
        ['views/fields/varchar'], function (Dep) {
    
        return Dep.extend({
    
            setup: function () {
                Dep.prototype.setup.call(this);
                
                this.listenTo(this.model, 'change', () => {
                    if (this.mode === 'list' || this.mode === 'listLink') {
                        if (this.model.hasChanged('isActive')) {
                            this.reRender();
                        }
                    }
                });
            },
    
            afterRender: function () {
                Dep.prototype.afterRender.call(this);
                console.log(this.model.attributes.id); // this print the id
                console.log(this.model.attributes.name); // this print the name
                console.log(this.model.attributes.isActive); // custom boolean field print undefined
    
                if (this.mode === 'list' || this.mode === 'listLink') {
                    if (this.model.get('isActive')) {
    
                        let isActive = this.model.get('isActive');
                        
                        let title = this.model.get('isActive') ? 'Active' : 'Inactive';
    
                        let $icon =
                            $('<i>')
                                    .css('margin-left', '2px')
                                    .addClass(isActive ? 'fas fa-check-circle fa-sm text-success'
                                        : 'fas fa-minus-circle fa-sm text-muted')
                                    .attr('title', title);
    
                        this.$el.append($icon);
                    }
                }
            },
    
            getAttributeList: function () {
                return ['name', 'isActive'];
            },
        });
    });

    I am not sure why this happening or if i am missing something here. by the way i have tried with another custom field and same result undefined.

    Any help or hints would b appreciated
    Rabii
    Web Dev
  • yuri
    Member
    • Mar 2014
    • 8453

    #2
    You might need this https://docs.espocrm.com/development...sdependencymap
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment


    • yuri
      yuri commented
      Editing a comment
      And you don't need this.listenTo here, it's excessive and can be a cause of side effects.
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    #3
    Thanks Yuri, i have added the selectDefs selectAttributeDependencyMap as below and also removed this.listen.To. But still the custom fields are not loaded on first initialization. This happen only when adding custom fields to core entities like Account, i tested similar code on a custom entity and all fields are loaded on first initialization.

    PHP Code:
    "selectAttributesDependencyMap": {
            "isActive": ["name", "accountNumber"]
     }

    Any help would be appreciated
    Rabii
    Web Dev

    Comment

    • yuri
      Member
      • Mar 2014
      • 8453

      #4
      When you select the name column (as your list layout has name), you need to let the backend to know to also fetch 'isActive' field. Since it's not on the list layout, it's not fetched with the list.

      Hence it should be "name": ["isActive"]

      Click image for larger version  Name:	image.png Views:	0 Size:	8.7 KB ID:	98573
      If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

      Comment


      • rabii
        rabii commented
        Editing a comment
        Many thanks
    Working...