Create simple and basic sugarcrm logic hook

By | January 20, 2012

In this example, I will explain how you create your first and very simple sugarcrm logic hook. This is just a basic example which will help you understand what logic hooks in sugarcrm can do. So, here we go.

What are we about to do?

Well, we are gonna create a simple logic hook that displays simple log entry to the database after creating a Opportunity record.

How to do?

First things first, take a backup of “custom/modules/Opportunities/logic_hooks.php” file if its already available. Now, just open the file “custom/modules/Opportunities/logic_hooks.php”, create it if its not already available. Add the following code into “custom/modules/Opportunities/logic_hooks.php”.

1
2
3
4
5
6
7
8
9
<?php
$hook_version = 1;
$hook_array['after_save'] = Array();
$hook_array['after_save'][] = array(1, 'log_data', 'custom/modules/Opportunities/log_data.php', 'logData', 'loggingData');
?>

The parameters of the hook_array are as follows:

1. Order of execution of logic hook
2. Name of the logic hook
3. Location of the logic hook
4. Class name of the logic hook.
5. Function to be called.

Now, create another file “custom/modules/Opportunities/log_data.php” and paste the following code. Now, this is the file, that will executed when the logic hook is triggered.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php

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

        require_once('data/SugarBean.php');

        class logData{
                function loggingData(&$bean, $event, $arguments){
                $login_user_name = $bean->assigned_user_name;
                $login_user_id = $bean->assigned_user_id;
                $GLOBALS['log']->info("MY-SAMPLE logic hook. Current LOGIN USER IS = $login_user_name and LOGIN USER ID = $login_user_id ");
                }
        }

?>

Now, under Admin –> System Settings, set the “Log Level” to Debug save it.

Go back to Admin –> System Settings and click on View Log. Click on “Mark Point” button.

Now, in another tab, open sugarcrm and create a new opportunity. After creating a new Opportunity, go back to the other tab and click on “Refresh From Mark” to find out your log entry.

If you like this article, then please consider sharing it. Sharing really inspires us to do things much better!!

4 thoughts on “Create simple and basic sugarcrm logic hook

  1. justtesting

    Hello,

    It is a nice article. I was wondering if you could help me with something. I want to save database data into a doc file using logic hooks. Is it possible ?

    Reply
    1. Vamsi M Post author

      Not sure, may be you should use triggers to save the database entries in another database and then use some scripts to save data in flat files.

      Reply
  2. Sumit Sengupta

    Is it possible to schedule email in sugarCRM ??

    What i mean is, any technique in sugarCRM that will send the email automatically to the desired destination without manually sending.

    Plz, suggest, if there is any way , i have tried , bt failed.

    Waiting for your response.

    Thankx in advance.

    Regards,

    Sumit Sengupta

    Reply
    1. Vamsi M Post author

      Hi, didn’t try it. May be you should work with any freelancer. Its been so long since I worked with sugarcrm.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *