Pages

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 app.data namespace. Here are a few examples that should help.

To load a single record from a given module:

var myRecord = app.data.createBean('Leads', {id:<record_id>});
myRecord.fetch();

Loading multiple records from a given module:

var myRecords = app.data.createBeanCollection('Leads');
myRecords.fetch();

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 = app.data.createBeanCollection('Leads');
leads.fetch({
        fields: ['id', 'status'],
        filter:[{'date_entered':{'$dateRange':'this_month'}}],
    });

Hope that helps!

7 comments:

  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?

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

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

      Delete
    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:

      app.api.call(, , {}, null);

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

      Delete
    4. Should be: app.api.cal(verb, endpoint_url, {}, null);

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

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

      Delete

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