Just like many of you, we wear a number of hats with one of them being developers. As part of that, we sometimes run into somewhat unique issues. Although this post is a slight deviation from the main line of dev tips, we feel it is something worth highlighting as it deals with how to handle unexpected issues when developing on SugarCRM.

TL;DR - If you are echoing out data to the client in a before_retrieve or after_retrieve logic hook, navigating via AjaxUI will break when attempting to go to the Detail View of any module that has a registered before_retrieve or after_retrieve logic hook. To get around this, use an after_ui_footer or after_ui_frame logic hook or echo your extra data elsewhere.

Recently while working on a client issue, I ran across a problem that kept occurring when trying to access a Contact's detail view. It would only fail when the page was being loaded with AjaxUI. It would error out with something similar to the following:

After examining the results of the ajax request to pull a contact's detail view, I noticed that the response was not valid json, preventing the client from properly dealing with the response. There was extra markup being added to the response. After searching for the contents of the extra markup in the codebase, I realized it was originating from an after_retrieve logic hook. There were multiple echo statements outputting data to the screen directly from the logic hook.

When Sugar introduced AjaxUI in SugarCRM 6.3, they did a decent job at handling most cases like this so that pre-6.3 customizations would still work with the AjaxUI changes. After trying to echo contents to the screen from other logic hooks (process_record, after_footer_ui, etc.), Sugar would work just fine. It would process the ajax request perfectly. It was only the detail views of *_retrieve hooks that have echo's that would break navigating with AjaxUI.

After digging deeper, it appears that the after_retrieve logic hook gets called for all modules deep into the application loading process of Sugar before an ob_start() call is made to start capturing anything echo'd. Because of this, as soon as you issue an echo in the logic hook, it gets sent to the client outside of the nicely formatted json response it's expecting.

If you'd like to replicate the issue for yourself, add an after_retrieve logic hook to a module and drop the following script in your /custom/modules/<module>/ folder. I'm using the Contacts module for the example and tested using SugarCRM CE 6.5.5.

With AjaxUI enabled, try to view a Detail View of a Contact. You should receive the same error I received above. If you don't receive an error and the page loads correctly, you should receive a javascript alert.

Having said all that, to solve the issue, I first attempted to run change from an after_retrieve hook to a process_record logic hook, but for whatever reason, process_record doesn't fire when you load a Detail View although the docs say it's suppose to.

The solution that ended up working was going with an after_ui_frame logic hook and explicitly catching the Detail View case as follows:

Like this post?

We'll send you an email once a month with the latest posts.

Keep up to date on the latest additions

We'll send you an email every month with handpicked add-ons, reviews, tricks and tips. Don't worry, we hate spam as much as you do.