Monday, August 10, 2015

SugarCRM Quick Hit: SugarBean and JavaScript

Sometimes in the course of customization work we may be performing within Sugar we may have a need to load one or more records from a module. 

For example, we may need to load an entry from the Calls module. Or, as demonstrated in a recent example I posted, we may need a set of records from the Leads module.

The Sidecar framework in Sugar 7 makes this process quite easy via the namespace. Here are a few examples that should help.

To load a single record from a given module:

var myRecord ='Leads', {id:<record_id>});

Loading multiple records from a given module:

var myRecords ='Leads');

Note the use of the fetch() method in both examples. It is used to actually instruct Backbone to execute the necessary calls to request the desired data via the Sugar REST v10 API. 

In the case of the first example, the JSON object passed as the second parameter ({id:<record_id>}) allows us to specify the ID of the record we wish to retrieve. However, in the second example, you will notice we are not specifying a similar property. 

When executed, the second example retrieves the first page of the same set of records that would otherwise be displayed to the current user should the user instead access the ListView for the same module. By default, that would be the first 20 records accessible to the current user, ordered by date_modified, descending.

But what if we wanted greater control over which records or fields are retrieved? That brings us back to the fetch() method mentioned earlier.

The fetch() method allows us to specify a number of additional parameters, including: filter, fields, success and others.

Here is an example of where the fields and filter parameters are defined and used to retrieve only the id and status fields for those leads created within the current month.

var leads ='Leads');
        fields: ['id', 'status'],

Hope that helps!


  1. How can I get the sum of all the related fields?

    If I have A related to B,I would like to get the sum of all the A.value. How to override the 20 item limitation?

    1. use parameter of limit .fetch({limit: -1}) // put any limit in place. -1 to remove the limit and return all data

  2. Replies
    1. Unfortunately, Sugar 6.5 does not have a native way of doing the same.

    2. Thganks Angel, can use javascript to call php function, that this function fetch related record value?

    3. The easiest way would be to create a custom endpoint out of the PHP function in question and then in JS you could do the following:, , {}, null);

      would represent the custom endpoint you added to the Sugar REST v10 API.

    4. Should be:, endpoint_url, {}, null);

      ...some of the text was stripped out in my initial reply

    5. I tried ajax call function in controller.php but it did not work


Your comments, feedback and suggestions are welcome, but please refrain from using offensive language and/or berating others. Thank you in advance.