Sugar Dev Tricks: Retrieving JavaScript Bean Models

Thanks for reading my first development entry.  If you’re new to developing on Sugar 7.x and 8.x, you might be a bit overwhelmed by the differences from previous versions of Sugar.  With these new versions we can take advantage of CRUD development directly on the client-side (browser) without having to write specific code in PHP.  With the new versions of Sugar, a new library called backbone.js was added.  You can take advantage of this robust library by using “beans” which are JavaScript objects that extends all the functionalities of a Backbone Model from SugarCRM.

Let’s say we want to retrieve a Lead record, with an ID of “ea68c454-851a-11e8-81e7-000d3af74b0f”, you can try the following codes:

var leadId = 'ea68c454-851a-11e8-81e7-000d3af74b0f'
var lead ='Leads');
lead.set('id', leadId);
lead.fetch({success: function(model, data){
console.log(model, data, lead); 

// this will  log 3 items in the dev console, model will be the updated model
// model will log the updated model, with the data updated from the ajax response
// data is the response object from the ajax call
// lead will be the backbone model we executed the fetch method (same value as model)
When should I use this?
  1. Let’s say we have a custom field that bases the value from a relate field (eg. relate_field + related_field_id), and you will need additional values from the relate_field’s record (eg. description, date_entered). You can fetch the record using from your custom field, specify on how you will display the retrieved values , and then call the render method.
  2. You can use this on Dashlets where you want to display the record on a specific sort of way. You can call the on the loadData() method of the dashlet, specify on how you will display the retrieved values, then call the render().

That’s it for my quick tip.  Thanks for reading!

0 Points