Trouble fetching related in record.js

Hello I am having a bit of trouble with getting data from a relate field in a custom record.js file. Below I have the code sample. I've been trying different data:sync:complete and render triggers. The function is called but I can't get the data out. 

this.collection.on('data:sync:complete', this.getRelatedFields, this);
this.model.on('data:sync:complete', this.getRelatedFields, this);
getRelatedFields: function () {
    var self = this;
    var closing_notary_id = self.model.get('apps_notaries_id1_c');
    debugger;
    if (!_.isEmpty(closing_notary_id)) {
        var closing_notary = app.data.createBean('apps_Notaries',{id: closing_notary_id});
        closing_notary.fetch();
        var phone = closing_notary.get('phone_work');
        self.model.set('closing_notary_phone_c',phone);

    }
},

When I use the Chrome console and call at the break point I get. But it works if I wait until the page has finished loading.@

stash.min.js:2 Uncaught TypeError: Cannot read property 'apply' of undefined 

I could use an after_retrieve logic hook instead but was hoping to use sidecar.

Any assistance would be appreciated.

Parents
  • I am not sure where the apply comes into it, but try waiting for the fetch to complete successfully. It is not uncommon for the code beyond the fetch to execute before the fetch is complete.

       var closing_notary_id = self.model.get('apps_notaries_id1_c');
        debugger;
        if (!_.isEmpty(closing_notary_id)) {
            var closing_notary = app.data.createBean('apps_Notaries',{id: closing_notary_id});
            closing_notary.fetch({
              success: function(
                var phone = closing_notary.get('phone_work');
                self.model.set('closing_notary_phone_c',phone);
              },
              error: function(e){
                console.log(e);
              },
            });
        }

    HTH,

    FrancescaS

  • I stay mostly on the server-side code, so I may not be a lot of help here.

    Is it possible 'app' variable on line 8 is out of scope? Maybe try SUGAR.App.data.createBean?

Reply Children
  • Hi Chad,

    I use app.data.createBean wherever needed... so I don't think that's the issue here, for what it's worth

     

    FrancescaS

  • Thanks for chiming in Chad. Francesca was right. I wasn't waiting for the fetch response in the code.

  • In case anyone else wants to do this here is the full record.js. In this case it is for Opportunities so it is located at:

    /custom/modules/Opportunities/clients/base/views/record/record.js

    ({
        extendsFrom: 'RecordView',
        initialize: function (options) {
         
            this.collection.on('data:sync:complete', this.getRelatedFields, this);

        },

        getRelatedFields: function () {
            var self = this;
            var closing_notary_id = self.model.get('apps_notaries_id1_c');
            debugger;
            if (!_.isEmpty(closing_notary_id)) {
                var closing_notary = app.data.createBean('apps_Notaries',{id: closing_notary_id});
                closing_notary.fetch({
                    success: function (data) {
                        var phone = closing_notary.get('phone_work');
                        self.model.set('closing_notary_phone_c',phone);

                    },
                    error: function(error) {
                        console.log(error);
                    }
                });

            }
        },
    })