Creating Logic Hooks for SugarCRM 5

There’s not much information out there about Sugar CRM, so here’s a quick example on creating a custom logic hook.

This example uses a custom helper function called ’sendSugarPHPMail’ which can be found here http://redinkdesign.net/open-source/simple-sugarcrm-e-mail-wrapper.

In this example, we’re going to email someone when a Case’s status is set to ‘Closed’.

To make this upgrade friendly we’re going to create two files in ‘custom/modules/Cases’.
First file: logic_hooks.php – Loaded automatically by Sugar.
Second: case_closed.php – Our custom code.

The contents of logic_hooks.php are:

<?php

	if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

	$hook_array = array();

	$hook_array['before_save'] = array();

	// array(hook execute order, 'hook name', 'hook code location', 'hook code class name', 'hook code function to execute')
	$hook_array['before_save'][] = array(1, 'case_closed', 'custom/modules/Cases/case_closed.php', 'case_closed', 'case_closed');

?>

Our array parameters must be the following:

1. hook execute order
2. hook name
3. hook code location
4. hook code class name
5. hook code function to execute

$hook_array['before_save'][] = array(1, 'case_closed', 'custom/modules/Cases/case_closed.php', 'case_closed', 'case_closed');

The contents of case_closed.php are:

<?php

	if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

	class case_closed{

		function case_closed(&$bean, $event, $arguments){

			// make sure the status was actually changed to closed
			// $bean->fetched_row['status'] is the stored status of the case
			// $bean->status is the status it was changed to
			if ($bean->fetched_row['status'] !== 'Closed' && $bean->status === 'Closed'){

				$subject = "Closed: Case {$bean->case_number} - {$bean->name}";
				$body = "Assigned to: {$bean->assigned_user_name}<br />";
				$body .= 'Description: ' . (empty($bean->description)? ' n/a ' : $bean->description);

				$to['Mr. Bean'] = 'mrbean@whatevs.com';

				if (!sendSugarPHPMail($to, $subject, $body)){
					$GLOBALS['log']->info("Could not send case closed notification:  " . $mail->ErrorInfo);
				}
			}
		}
	}

?>

Application hooks

These hooks do not make use of the $bean argument.
after_ui_frame
Fired after the frame has been invoked and before the footer has been invoked
Available in version 4.5.0 and later
after_ui_footer
Fired after the footer has been invoked
Available in version 4.5.0 and later
server_round_trip
Fired at the end of every SugarCRM page
Available in version 4.5.0 and later

Module hooks

before_delete
Fired before a record is deleted
Available in version 4.5.0 and later

after_delete
Fired after a record is deleted
Available in version 4.5.0 and later

before_restore
Fired before a record is undeleted
Available in version 4.5.0 and later

after_restore
Fired after a record is undeleted
Available in version 4.5.0 and later

after_retrieve
Fired after a record has been retrieved from the database. This hook does not fire when you create a new record.
Available in version 4.5.0 and later

before_save
Fired before a record is saved.
Note: With certain modules, like Cases and Bugs, the human-readable ID of the record (like the case_number field in the Case module), is not available within a before_save call. This is because this business logic simply hasn’t been executed yet.
Available in version 4.5.0 and later

after_save
Fired after a record is saved.
Note: With certain modules, like Cases and Bugs, the human-readable ID of the record (like the case_number field in the Case module), is not available within a before_save call. This is because this business logic simply hasn’t been executed yet.
Available in version 4.5.0 and later

process_record
Fired immediately prior to the database query resulting in a record being made current. This gives developers an opportunity to examine and tailor the underlying queries. This is also a perfect place to set values in a record’s fields prior to display in the DetailView or ListView. This event is not fired in the EditView.
Available in version 4.5.0 and later

Hooks for Users module

before_logout
Fired before a user logs out of the system
Available in version 4.5.0 and later

after_logout
Fired after a user logs out of the system
Available in version 4.5.0 and later

after_login
Fired after a user logs into the system.
Available in version 4.5.0 and later

after_logout
Fired after a user logs out of the system.
Available in version 4.5.0 and later

before_logout
Fired before a user logs out of the system.
Available in version 4.5.0 and later

login_failed
Fired on a failed login attempt
Available in version 4.5.0 and later

Here’s a list of all available logic hooks…

http://developers.sugarcrm.com/docs/OS/5.1/-docs-Developer_Guides-Developer_Guide_5.1-DevGuide.1.101.html

    • DAniel
    • August 22nd, 2008

    Hallo

    The tutorial seemed good, but onimplementing it it did not work

    • Anonymous
    • October 9th, 2008

    Hi,

    I try to use this, but get this error:

    [09-Oct-2008 14:57:36] PHP Fatal error: Call to undefined function sendSugarPHPMail() in custom/modules/Cases/case_closed.php on line 15

    Are you using a changed version of the sugar mail function or you change something more?

    • Ashwin Surajbali
    • October 14th, 2008

    My apologies, the function sendSugarPHPMail() is a custom function I wrote to send an email using Sugars built in email capabilities. That should be the only problem. I created another page for that helper function at http://www.redinkdesign.net/sugar_email_wrapper

    • cmayes
    • January 21st, 2009

    Thanks for the insight. Still not able to populate with the case number and user (the user id seems to be alright, but not the username – don’t need the full name), don’t know whats going on.

    Also, do you know of a way that I might be able to configure something to operate with my Joomla/Sugar bridge that will send out the email notification only if certain rules apply? For instance, I want the email to go out only if the case is created via the Joomla/Sugar bridge but NOT if created inside of Sugar alone.

    • Jason
    • January 12th, 2009

    Hi There,

    The line above “$to['Mr. Bean'] = ‘mrbean@whatevs.com’;”

    What would this need to be changed to to represent the original emailer of the issue? I’d prefer to send it back to the originator of the issue rather than an email of my choosing.

    Other than that, it works.

    Thanks

    • Ashwin Surajbali
    • January 12th, 2009

    If you do var dump on $bean, you should see the email info in there.

    Try this


    echo '

    ';
    print_r($bean);
    echo '

    ';

    See what get’s printed out and look for your desired variable.

    • cmayes
    • January 21st, 2009

    This is an awesome hook, thanks for making it available.

    I edited it a bit so that emails are sent when cases are created.

    Using it in conjunction with a bridge for Joomla and Sugar, but cannot figure out how to make it populate the case number created and who it is assigned to. (The email merely says in the subject: “New: Case” and I would like it to say “New: Case #[whatever]” and then in the actual email say who it is assigned to.) Any pointers?

    • Ashwin Surajbali
    • January 21st, 2009

    Hey try this for the subject…


    $subject = "New: Case # {$bean->case_number}";

    For the assigned user, you can get the assigned user id from the bean $bean->assigned_user_id. To get the actual name of the user, you can do a custom query on the database or do something like the following ( code not tested )


    include_once('modules/users/User.php');
    $user = new User();
    $user = $user->retrieve($bean->assigned_user_id);
    $user_name = $user->user_name;

    • Anonymous
    • February 2nd, 2009

    Hello

    The tutorial seemed good, but onimplementing it it did not work

    • Ashwin Surajbali
    • February 5th, 2009

    What errors did you get?

    • Anonymous
    • February 15th, 2009

    This is an awesome hook, thanks for making it available.

    I edited it a bit so that emails are sent when cases are created.

    • Anonymous
    • March 6th, 2009

    Yeah it version 5.2 it doesn’t work any more, it worked only in 5.0

    • Anonymous
    • May 6th, 2009

    Hey this is not working for me, when i replace the code for sending the mail with the print_r(‘Not working’) …… it doesnt print anything
    i am wrong somwhere …..please suggest

    • Ashwin Surajbali
    • May 6th, 2009

    I still use this code in a 5.2 production setup…are you getting any errors?

    • Ashwin Surajbali
    • May 6th, 2009

    Make sure your logic hook is defined properly and that this code is actually being executed. Turn on PHP error reporting and take a peek…

  1. Hi, thanks for the codes, I have used it in version 5.0i, I edited some codes in the email wrapper, it works perfect..

  2. I mean version 5.2.0i it worked for me

    • Rakesh
    • June 7th, 2010

    neighter error is coming nor email is sending.
    The code should be written & instructed properly

    • Ashwin Surajbali
    • June 7th, 2010

    @DAniel
    Please make sure you’re using the email wrapper function.

    • Ashwin Surajbali
    • June 7th, 2010

    Rakesh :
    neighter error is coming nor email is sending.
    The code should be written & instructed properly

    Make sure you’re using the email wrapper function, there’s a link at the top of this post.

  1. No trackbacks yet.