Wednesday, July 27, 2011

Logic Hooks: Truncating Displayed Values

Sometimes minor customizations to SugarCRM go a long way towards addressing important usability aspects. Something that has come up a couple of times in recent interactions with users is the need to truncate data displayed on a subpanel.

At first glance, the reasons why someone would want to do this are not always readily apparent. However, consider the scenario where one uses a text area field (e.g. description) on a module and one wishes to display that field on the subpanel.

The process of including the field on the subpanel is easily accomplished via Studio, but over time, an issue not initially apparent usually starts to surface.

Because a text area field can hold large amounts of text, it is possible for a user to populate the field with a number of pages worth of data. That on its own is not a problem. However, displaying that on a ListView, of which a subpanel is a good example, usually causes undesired display problems, as demonstrated by the image that follows (click to enlarge):

The issue at hand is that the Description field with the longer text value makes the subpanel appear a bit odd, to the point that the additional columns that are part of the view appear to the extreme right of the screen.

So how do we solve this? Easily, via a logic hook. Let us take a look at how we do so.

First of all, lets talk a little bit more about our example from the previous image. 

The image illustrates the default Member Organizations subpanel one finds on the DetailView of an Account record. Thus, the description field in question in this example belongs to the child Account records associated with the current Account. 

In addition, this bit of knowledge also tells us that the logic hook we need to build will be for the Accounts module, thus, the related files will reside in the folder <sugar>/custom/modules/Accounts.

Let us build the files.

The first file is the logic hook definition file (logic_hooks.php):


$hook_version = 1; 

$hook_array = Array(); 

$hook_array['process_record'] = Array(); 
$hook_array['process_record'][] = Array(1, 'DescTruncate', 'custom/modules/Accounts/DisplayUtils.php','DisplayUtils', 'truncateDesc'); 

Next is the DisplayUtils.php file, referenced in the above logic hook definition file:


Project: Logic Hooks for Truncating Displayed Value
Original Dev: Angel Magaña, July 2011
@2009-2011 Angel Magaña

Desc: Logic Hooks for Truncating Displayed Value

The contents of this file are governed by the GNU General Public License (GPL).
A copy of said license is available here:
This code is provided AS IS and WITHOUT WARRANTY OF ANY KIND.

class DisplayUtils {

function truncateDesc (&$bean, $event, $arguments)
$bean->description = substr($bean->description, 0, 100);



Our final step is to save both of the files and place them in the folder <sugar>/custom/modules/Accounts

Assuming everything is fine, the subpanel should now be nicer and easier to read, as demonstrated in the image (click to enlarge):

Via this technique, you are at liberty to manipulate the displayed data in a number of different ways.


  1. Hi, I am using your code without success

  2. Hi Angel, thank you for this code, it works fine, I was missed in a wrong module but I change the path and now it is all rigth.



  3. This is exactly what I was looking for. Works great. Muchas gracias Angel.


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