One Way To Index ElasticSearch In Sugar 7

Edit (8/20/14): As Cédric Mourizard mentioned in the comments, if you have access to CLI you can simply run the following command to trigger an indexing of ElasticSearch. The method explained in this article is geared toward triggering an indexing via the API in scenarios where you may not have access to the CLI.

$ php silentFTSIndex.php

Like many, I’m comfortable with the ETL tools that I have been using in Sugar for years now. While many ETL tools support Sugar’s SOAP API (and a couple support the new REST API), I often prefer working directly with the database as it’s a much quicker way to move large amounts of data due to bypassing business logic such as workflow rules and logic hooks.

Things changed in 7. With ElasticSearch now being a requirement of Sugar, doing database work can have some undesired effects in regards to search and activity stream functionality. They simply do not work if data has been imported directly to the database.

One way I have found to update ElasticSearch after doing an import directly to the database is to interact with the bean within Sugar or using the API.

I think it’s safe to assume that our goal as Sugar developers is to automate tedious tasks so that users (and us developers) can work more efficiently. Indexing ElasticSearch is certainly one of those tasks.

That said, I find writing a script using the Sugar 7 Rest Client to be a quick and easy way to index ElasticSearch after performing Sugar 7 database work.

Here is a quick example of using the Sugar 7 Rest Client to do just that.

Have a better idea? Please let me know in the comments below!

Convert All MyISAM Tables To InnoDB

Today I took on the task of converting our SugarCRM MySQL MyISAM tables to InnoDB. Currently we have roughly 200 tables in the SugarCRM DB. Its a time consuming processes to convert each table one at a time. Luckily, using some SQL scripting you can make this much less painful.

Start by running the following query to build a list of SQL statements that will handle converting each table.

The output generates a query for each table to be converted to InnoDB.

Copy and paste these results and run the queries. After some time (in my case about 20 minutes) all of the tables in the DB will be converted to InnoDB.

Role Specific Drop Downs in SugarCRM

Today I ran in to a scenario where I needed the status field for projects to hide a value for everyone except two specific roles. I did some googling and found a post which seemed to theoretically accomplish what I was looking for. However, in practice it didn’t. That said, I won’t be covering what didn’t work as nobody really cares.

Now down to business. The first thing we need to do is edit the vardefs of the drop down to use a custom function that will handle the logic of determining if the specified value is available for selection.

Next, create the file and function to handle the logic.

Finally, run Quick Repair and Rebuild. The drop down is now hiding the “Pending Input” option for everyone except users in the Developer and Marketing roles.

Introduction to the Sugar 7 API Wrapper Class

In a previous post I showed you how to use Guzzle to interact with the new SugarCRM v10 API. Today, I would like to introduce you to an even easier way to utilize the new API, the SugarCRM REST API Wrapper Class.

Installation and Instantiation

Installation of this package is handled with Composer. Start by updating composer.json with the following information.

Next, install the package via Composer.

With the SugarCRM REST API Wrapper installed and auto loaded, we can now instantiate the class and authenticate against our SugarCRM instance.


Common Usage Examples

1. Searching for an account by Name.

2. Alternatively, we can retrieve an account by ID.

3. Retrieving a list of cases associated to an account.

4. Create a case and relate the case to an account.

5. Listing notes associated to a case.

6. Listing documents associated to the account. (Compare to using Guzzle without the wrapper class)



There you have it. Using this new wrapper class is very simple and involves much less code than using cURL or even Guzzle to interact with the new API. That said, the wrapper class requires Guzzle and will install it via composer for you.

Are there any other examples you would like to see? If so, please comment and I’ll update this post accordingly.

SugarCRM Logic Hook Improvements

Edit 1: In response to comments from Jason Eggers I have updated my closure concept to include labels so that they can be identified and uninstalled.

Edit 2: Chad Hutchins further contributed to this concept with a basic outline of a helper class. This post has been updated accordingly.

Like a lot of you SugarCRM dev’s, I find myself creating simple logic hooks fairly often. While this is a simple task I feel that it could be even simpler. Lets look at the current process of creating a logic hook.

1. Create the file /custom/modules/{module}/logic_hooks.php

2. Create your logic hook class at /custom/modules/{module}/logic_hooks_class.php

Two easy steps. That’s all it takes. While this is pretty straight forward I cant help but feel like its an unnecessary amount of work for executing 1 function.

If logic_hooks.php were to support closures (PHP 5.3.0 +) we could achieve all of the above in 1 file (logic_hooks.php) and eliminate the need for the “logic_hooks_class” class all together.

Here is a brief example of how it could potentially work:

In 1 file and roughly 10% of the initial code required we have included all of our logic which makes the logic hook easier to maintain and quicker to write. Personally, this would eliminate at least 75% of my logic hook related files.

Somewhere deeper in the SugarCRM platform this code could be executed with something along the following lines:

To further make using logic hooks more efficient Chad Hutchins has contributed the following helper class concept to make adding and removing logic hooks that much easier.

Thoughts? Opinions? I would love to hear some feedback on this concept!

Using SugarCRM’s New RESTful API With Guzzle

Today I saw a post from Mr. John Mertic that described how to interact with the new API via cURL. While it’s a great piece of information, I find working with the Guzzle library to be much more efficient due to Guzzle’s support for event listeners. That and I hate working directly with cURL.

That said, lets get down to business and start working with the new API.

First, lets update composer.json to install Guzzle

    "require": {
        "guzzle/guzzle": "~3.1.1"

Next, install the library via composer

$ composer install

Now with Guzzle auto loaded we can start to have our fun with the new API.

use Guzzle\Common\Event;
use Guzzle\Http\Client;

// specify the REST web service to interact with
$url = 'http://sugarinstance/rest/v10';

// And admin username/password
$username = 'username';
$password = 'password';

$client = new Client($url);

//Authenticate and retrieve an OAuth2 Token
$request = $client->post('oauth2/token', null, array(
  'grant_type' => 'password',
  'client_id' => 'sugar',
  'username' => $username,
  'password' => $password,

$results = $request->send()->json();

$token = $results['access_token'];

//Register an Event Listener to automatically include 
//the OAuth2 token on all requests to the REST web service
  'request.before_send', function(Event $event) {
    $event['request']->setHeader('OAuth-Token', $token);

//Retrieve all documents related to the specified account
$request = $client->get('/Accounts/' . $account_id . '/link/documents');
$result = $request->send()->json();


As you can see, using Guzzle makes interacting with the new API a breeze and has an added bonus of making the code much easier to understand.

Stay tuned, on my next post I’ll show how you can use the SugarCRM 7 API Wrapper Class to accomplish the same task in just a few lines of code.