Pages

Thursday, February 18, 2010

SugarCRM Logic Hook: Proper Casing Fields

Sorry for my absence. Been meaning to write, but been caught up with other cool things.  Anyway, wanted to post a quick and easy one that many might find helpful. 

One of the biggest challenges with a database application such as SugarCRM is ensuring that data is entered in a uniform manner by its various users. The use of drop down and multi-select fields helps address this challenge quite well, but said field types cannot be used in all places. 

A good example of where they cannot be used is the Last Name field on a Contact entry. However, at the same time, it would be nice to be able to enforce some uniformity in the field's values, for example, ensure that names are being entered in proper case.  

This need provides us with a great opportunity to once again highlight the power of logic hooks. In our example, the uniformity we want to introduce is the proper casing of the text entered into the Last Name field. Thus, values that are typed in as "doe" are automatically corrected and saved to the database as "Doe."

A logic hook is the perfect solution as it is transparent to the user and easily extended to support other fields.

Lets take a look at how we can accomplish the above...

First of all, lets begin by creating our logic hook definition file.  This file is always named logic_hooks.php and for our example, will be stored in custom/modules/Contacts

The contents of logic_hooks.php are as follows:

<?php

$hook_version = 1;

$hook_array = Array();
$hook_array['before_save'][] = Array(1, 'ProperTest', 'custom/modules/Contacts/proper_test.php', 'ProperText', 'convertToProper');

?>

Next we need to create the file that will contain the code that actually does the work of converting the field's value to proper case.  That file will be named proper_test.php and also resides in the custom/modules/Contacts folder.

proper_test.php will contain the following:

<?php

/*************************************
Project: Sample Hook to Proper Case a Field's Value
Original Dev: Angel Magaña, February 2010
cheleguanaco[at]gmail.com

Desc: Logic Hook Code for Proper Case Update 

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

*************************************/

class ProperText {

 function convertToProper(&$bean, $event, $arguments) 
 {
  $bean->last_name = ucwords($bean->last_name);
 }

}

?>

To test, enter a new contact and type a value in the Last Name field that is not proper cased, then save the contact.  Note that when saved, the contact's last name value is automatically adjusted.

That's all it takes!

8 comments:

  1. hi,
    above logic hook example is good.
    but what my intention is to write the more than one logic hooks at a time...
    is it possible?
    give me some example for different logic hook events...(after_save,after_retrieve,process_record,etc...)
    if u r free..
    can i contact u through skype id:
    my id : ramanjaneya987

    ReplyDelete
  2. Buenas he intentado con todos los ejemplos que he conseguido sobre logic hooks. y ninguno me funciona.

    Me doy cuenta que hacen referencia a ./custom/modules.

    Y mi sugarcrm el custom no tiene carpeta alguna.. modules es independiente.

    Necesito un poco de ayuda, porque quiero utilizar esta herramienta poderosa que tiene sugar :D

    ReplyDelete
  3. @FeCr_88: Si no existe la carpeta, se tiene que crear y luego poner los dos archivos en la carpeta.

    ReplyDelete
  4. nice article,,,
    more useful

    how to connect with Database

    Please guide me

    My skype ID: gurunathan.s

    ReplyDelete
  5. @Gurunathan

    You need to elaborate on your question as there are many ways to connect to the database.

    ReplyDelete
  6. Hi Angel,

    I never found this to be so critical, even one of my client had this requirement, but we used ucfirst....i guess ucwords will upper case all the words within the string.

    Great post though..since its your blog...lol :)

    ReplyDelete
  7. @mangesh: It all depends on what people need/want. There are just as many valid reasons for using ucfirst as there are for using ucwords. It all comes down to which best suits the scenario.

    ReplyDelete
  8. Hi! When I create new opportunity from contacts in Sugar 6.2 pro - in opp lead_source is defoult, but we need that lead source is identical lead_source in contact. Please Help me!!!!

    ReplyDelete

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