Wednesday, July 13, 2011

Updating the date modified field on SugarCRM when saving a note using triggers

About SugarCRM
SugarCRM is an opensource CRM solution made with php and mysql, it is also available in a paid support version. It is a solid CRM with tons of features and some add-ons available. It is highly customizable for many types of businesses to work with.

Why update date modified?
CRM's can be used in a number of different ways, in many respects how the system is set up and the processes the users establish dictate how useful the CRM will be. In this case the system was pre-existing and the sales force has been using it for quite some time, so I didn't really want to try and change their process.

In SugarCRM the accounts screen view generates a table with numerous columns, one of which is the date modified which can be used for sorting. The sales team liked to use this to see which of their clients they had last worked with; however saving a note does not change the date modified. Only updating the main record updates the the data modified in the database, so they would create a note then do a save on the record to force a change on the date modified field.

I researched a number of solutions, there was logic hooks, custom fields and a number of things people have done but I had a simple idea... database triggers.

Basically I used the following statements:

create trigger note_insert insert on notes for each row update accounts set accounts.date_modified = NEW.date_modified where accounts.id= NEW.parent_id
create trigger note_update after update on notes for each row update accounts set accounts.date_modified = NEW.date_modified where accounts.id= NEW.parent_id
Now when some one creates or updates a note the date modified is changed by mysql and I didn't even have to touch the SugarCRM code. I know from searching around that others have made more elaborate changes to SugarCRM for similar purposes but this worked for me so I share it with you.