How can I access fields in my javascript?

Hi,

Sorry if the question is very simple but I'm can´t seem to find a solution to my problem, maybe because I'm new to SugarCRM development. I managed to add a button near the "Edit" button when you open a contact and I managed to make it call a hardcoded phone number through the app I needed.

The problem was that when I tried to make the javascript that does what I previously described use the phone number field present in the current contact, I simply couldn't find a way. How can I store the contact that I have opened in a var for later use in the javascript?

Thanks in advance!

Parents
  • You can access values of a field via JavaScript using the following syntax:

    this.model.get(<field_name>);

    Note that you will want to make sure that you do that *after* the data:sync:complete event fires or after render, otherwise you might get an undefined as the data might not yet be loaded.

  • I'm clearly missing something really basic here, here is the script that I'm executing:

    (function(app){

        app.events.on('app:sync:complete', function(){

            app.router.on('route:record', function(module){

                if(module === 'Contacts') {

                    app.controller.context.on('button:call_button:click', function(model){

                        var mobilePhone = this.model.get('phone_mobile');

                        console.log("Call button clicked, phone number:" + mobilePhone);

                       // AJAX POST

                    });

                }

            });

        });

    })(SUGAR.App);

    Sorry if this isn't the best place to post code and the error is:

    Uncaught TypeError: Cannot read property 'get' of undefined

    Thanks for the quick reponse!

  • The context for your event callback (essentially, the value of 'this' keyword) is different than it would be if your code was running from a custom Sidecar controller.

    You can try using app.controller.context.get('model') to access the current model object in the Record view.

    App Ecosystem @ SugarCRM

  • inside the router you cant access "this"

    TRy this out:

    (function(app){

    var that = this; // gos outside the router

        app.events.on('app:sync:complete', function(){

            app.router.on('route:record', function(module){

                if(module === 'Contacts') {

                    app.controller.context.on('button:call_button:click', function(model){

                        var mobilePhone = that.model.get('phone_mobile');

                        console.log("Call button clicked, phone number:" + mobilePhone);

                       // AJAX POST

                    });

                }

            });

        });

    })(SUGAR.App);

Reply Children
No Data