Email Logic Hook - After save

Hello,

I would like to execute a specific code after an email is received. The code parses the email subject and do specific actions. I would like a more custom way than binding the email with a case from the subject ( [Case:XXX] to Case XXX)
However it seems that the hook is never executed.

I use sugar 7.7.2

I added those files and ran "Quick Repair and Rebuild".

./custom/Extension/modules/Emails/Ext/LogicHooks/test.php

<?php

   $hook_version = 1;

   $hook_array['after_save'][] = array(
      1,
      'Create case or add to case',
      'custom/modules/Emails/Emails_hook.php',
      'email_utils',
      'link_to_case'
);

./custom/modules/Emails/Emails_hook.php

<?php

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

   class email_utils
   {
      function link_to_case($bean, $event )
      {
         $GLOBALS['log']->debug('-- Link to case executed');

         // code goes here

      }

   }

?>

What am I doing wrong here ?

Thank you for your help

  • Hi Bastien,

    I have found two missing pieces in your logichook definition.
    1.Please initialize array variable $hook_array like below.
    2.Please initialize array variable $hook_array['before_save'] like below.

    Logichook definition will look like something below.

    <?php
    $hook_version = 1;
    $hook_array = Array();
    $hook_array['before_save'] = Array();
    $hook_array['before_save'][] = Array(
    1,
    'Store values',
    'custom/modules/<module>/My_Logic_Hooks.php',
    'My_Logic_Hooks',
    'before_save_method'
    );
  • Hi Bastien,

    I am not sure what you meant with: "I would like a more custom way than binding the email with a case from the subject ( [Case:XXX] to Case XXX)" as that's just a setting that can be configured out of the box on the Admin area:

    If your needs are around custom case or email (during case creation) assignments/visibility/notification, have a look if this old blog post I wrote still works and if it can help you: Use SugarCRM as your company's Ticketing system | Enrico Simonetti [dot com] 

    Cheers

    --

    Enrico Simonetti

    Sugar veteran (from 2007)

    www.naonis.tech


    Feel free to reach out for consulting regarding:

    • API Integration and Automation Services
    • Sugar Architecture
    • Sugar Performance Optimisation
    • Sugar Consulting, Best Practices and Technical Training
    • AWS and Sugar Technical Help
    • CTO-as-a-service
    • Solutions-as-a-service
    • and more!

    All active SugarCRM certifications

    Actively working remotely with customers based in APAC and in the United States

  • Thank you for you help Ajay.
    I actually follow this documentation and there was no initialization: http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.7/Architecture/Logic_Hooks/#Hooks_with… 

    I added those two lines as you advise me (using after_save instead of before_save) :
          $hook_array = array();
          $hook_array['after_save'] = array();

    After a "Quick Repair and Rebuild" and receiving an email on my configured address still nothing is happening. I don't see my debug message and I have got no php error.

  • Hi Enrico, thanks for your input.

    What I want to achieve is to assign an email to a case and link that case to a customer based on the subject (for example the subject contain the customer number). If the email is a reply I want to bind it to the same case. Otherwise I make a new case.

    Email archiving (http://support.sugarcrm.com/Knowledge_Base/Email/Enabling_Sugar_Email_Archiving/index.html ) is close to what I want to do but I don't want to link all emails directly to an account. I want to group emails by conversation (re: ...). Each conversation is bind to a separate case.

    Furthermore the email sender is not the customer so I cannot based the archiving on the sender.

    I am not sure your code suits my need.

  • I gave more details on what I want to achieve below if this can help.

  • Hi Bastien,

    I would have a look at the following two methods and see if their logic can be adapted:

    Especially with the second one (line 80) you should be able to change the Case assigned to the email as it is before save on the second save of the email, when the parent of the Email is changed from nothing to Cases.

    That's where you can put your custom logic that handles different cases, or additional logic in general.

    Cheers

    --

    Enrico Simonetti

    Sugar veteran (from 2007)

    www.naonis.tech


    Feel free to reach out for consulting regarding:

    • API Integration and Automation Services
    • Sugar Architecture
    • Sugar Performance Optimisation
    • Sugar Consulting, Best Practices and Technical Training
    • AWS and Sugar Technical Help
    • CTO-as-a-service
    • Solutions-as-a-service
    • and more!

    All active SugarCRM certifications

    Actively working remotely with customers based in APAC and in the United States

  • Hi Enrico,

    I am gonna try this later but first I wanna make sure the hook is triggered, which is not the case yet.
    Thanks for your time.

  • Try:

    1. Check your PHP error log for errors
    2. Check if the file 

      custom/modules/Emails/Ext/LogicHooks/logichooks.ext.php (I am almost sure that's the right path) contains your hook definition

    3. Add an additional parameter to your logic hook method, it needs 3 parameters, not 2 ($bean, $event, $arguments)
    4. Add "public" on the method definition
    5. Modify logging from "debug" to "fatal" on your method and verify that you actually display fatal on the Admin settings
    6. Check that the sugarcrm.log file is writable and make sure that changing the system log level does write to the file

    --

    Enrico Simonetti

    Sugar veteran (from 2007)

    www.naonis.tech


    Feel free to reach out for consulting regarding:

    • API Integration and Automation Services
    • Sugar Architecture
    • Sugar Performance Optimisation
    • Sugar Consulting, Best Practices and Technical Training
    • AWS and Sugar Technical Help
    • CTO-as-a-service
    • Solutions-as-a-service
    • and more!

    All active SugarCRM certifications

    Actively working remotely with customers based in APAC and in the United States

  • Thank you Enrico

    1. No error in my php log. The log is effectively writable.
    2. I have no custom/modules/Emails/Ext folder. (I do have it for other module : /custom/modules/Calls/Ext/LogicHooks/logichooks.ext.php)
    3. I added the third parameter
    4. I added public
    5. What is the purpose of restricting log to fatal error ?
    6. Yes the sugarcrm.log is writable.

    Unfortunately still not working...

  • No problem.

    #2 check for overall folder permissions, repair again and see if it works. If not check if there is something wrong with the naming conventions (eg: misspelling, incorrect case etc). I think that might be the problem at this point

    #5 it was to make sure that you do not get lost with all the "noise" on the debug level log, if you reduce the log level and increase the logging of the method you will only see what is actually happening on your code

    If you have a copy of the system (obviously don't use your production), give it a test with my code and add some logging there, if that works, there is definitely something wrong with the current code.

    --

    Enrico Simonetti

    Sugar veteran (from 2007)

    www.naonis.tech


    Feel free to reach out for consulting regarding:

    • API Integration and Automation Services
    • Sugar Architecture
    • Sugar Performance Optimisation
    • Sugar Consulting, Best Practices and Technical Training
    • AWS and Sugar Technical Help
    • CTO-as-a-service
    • Solutions-as-a-service
    • and more!

    All active SugarCRM certifications

    Actively working remotely with customers based in APAC and in the United States

1 2