Hi ! New user ! Need helps on LogicHooks :)

Hi everybody,
I’m new on SuiteCrm and i’m trying to have an age calculation in a field.
I’m using the last version (7.7).

To do this, i read a lot of doc and posts in the forum and i try this.
First, I have modified the file custom/modules/Contacts/logic_hooks.php like this :

<?php
// Do not store anything in this file that is not part of the array or the hook version.  This file will	
// be automatically rebuilt in the future. 
 $hook_version = 1; 
$hook_array = Array(); 
// position, file, function 
$hook_array['before_save'] = Array(); 
$hook_array['before_save'][] = Array(1, 'Contacts push feed', 'modules/Contacts/SugarFeeds/ContactFeed.php','ContactFeed', 'pushFeed'); 
$hook_array['before_save'][] = Array(77, 'updateGeocodeInfo', 'modules/Contacts/ContactsJjwg_MapsLogicHook.php','ContactsJjwg_MapsLogicHook', 'updateGeocodeInfo'); 
$hook_array['before_save'][] = Array(1, 'calcul age', 'custom/modules/Contacts/calculage_LogicHook.php','calculageclass', 'calculagefunction');

$hook_array['after_save'] = Array(); 
$hook_array['after_save'][] = Array(1, 'Update Portal', 'modules/Contacts/updatePortal.php','updatePortal', 'updateUser'); 
$hook_array['after_save'][] = Array(77, 'updateRelatedMeetingsGeocodeInfo', 'modules/Contacts/ContactsJjwg_MapsLogicHook.php','ContactsJjwg_MapsLogicHook', 'updateRelatedMeetingsGeocodeInfo'); 


?>

So i only add this line :

$hook_array['before_save'][] = Array(1, 'calcul age', 'custom/modules/Contacts/calculage_LogicHook.php','calculageclass', 'calculagefunction');

After that, i create a new file custom/modules/Contacts/calculage_LogicHook.php and put this inside :

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

class calculageclass {
        // Local static variable to store your sum
        protected static $total = 0;

        function functionToSumYourField($bean, $event, $arguments) {
                // do any processing and calculations you want before summing your values here
//CALCUL DE L'AGE
//recup anniv
$anniv = $bean->birthdate;


$agecalc = floor(divide(subtract(daysUntil(today()),daysUntil($anniv)),365.242));

//update
                $bean->age_c = $agecalc;
                $bean->save();

After that, i do a quick repair to test it, but this not works…
The goal is in the contact to have from my field birthdate the calculation of the age of a person in the field age_c.

Can someone help me to understand why this is not working and how to get this working ?
Thanks for the help :slight_smile:
Xavier

Salut,

The file name must have the same class name. And you don’t need to do “bean->save” (sais plus mais je crois).
Perso j’utilse des noms :

contacts_logic_hooks.php
class contacts_logic_hooks

et la la events : before_save(){} etc.

Regards

Salut :slight_smile:
Je continue en anglais (puisqu’on est sur le fil en anglais).

To help me, could you show me a piece of code starting on my own code to help me to understand ?
If i apply what you say, i should have this (i will apply your advice on the name to recognize “logic_hook” after. I would like to see if i can get something working…) :

  • the custom/modules/Contacts/logic_hooks.ph does’nt change (right ?)
  • i have to rename custom/modules/Contacts/calculage_LogicHook.php to custom/modules/Contacts/calculage.php (right ?)

In the calculage.php, i have then :

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

class calculageclass {
        // Local static variable to store your sum
        protected static $agecalc = 0;

        function functionToSumYourField($bean, $event, $arguments) {
                // do any processing and calculations you want before summing your values here
//CALCUL DE L'AGE
//recup anniv
$anniv = $bean->birthdate;
//calcul pour avoir age depuis date de naissance
$agecalc = floor(divide(subtract(daysUntil(today()),daysUntil($anniv)),365.242));
//mise à jour du champ age_c
                $bean->age_c = $agecalc;
                

Right or i miss something ?
Thanks :slight_smile: / Merci :slight_smile:

[EDIT] i did not understand your advice here : “… et la la events : before_save(){} etc…”

check the documentation regarding logic_hooks http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_6.5/Module_Framework/Logic_Hooks/Module_Hooks/before_save/

<?php

    $hook_version = 1;
    $hook_array = Array();

    $hook_array['before_save'] = Array();
    $hook_array['before_save'][] = Array(
        //Processing index. For sorting the array.
        1,
       
        //Label. A string value to identify the hook.
        'before_save example',
       
        //The PHP file where your class is located.
        'custom/modules/{module}/logic_hooks_class.php',
       
        //The class the method is in.
        'logic_hooks_class',
       
        //The method to call.
        'before_save_method'
    );

?>
<?php

    if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
   
    class logic_hooks_class
    {
        function before_save_method($bean, $event, $arguments)
        {
            //logic
        }
    }

?>

the class and the function should be the same in logic_hooks.php that’s what you’re missing

Hi :slight_smile:
And that’s all ??
Ok, i’m goind to try this…
I will come back to tell you if i got something working :wink:

Thanks to everybody :slight_smile:
Xavier

Hi.
Well, i try by myself and obviously i’m missing something.
I get a white page and nothing in the logs. This is what i have now :

  • the custom/modules/Contacts/logic_hooks.ph with this line inside :
$hook_array['before_save'][] = Array(1, 'calcul age', 'custom/modules/Contacts/calculageclass.php','calculageclass', 'calculagefunction');
  • the custom/modules/Contacts/calculageclass.php with this code :
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class calculageclass {
        // Local static variable to store your sum
        protected static $total = 0;

        function calculagefunction($bean, $event, $arguments) {

//CALCUL DE L'AGE
//recup anniv
$anniv = $bean->birthdate;


$agecalc = floor((time() - strtotime($anniv))/31556926);
//mise à jour du champ age_c
                $bean->age_c = $agecalc;
        } // end of calculagefunction

?>

So for the pros, that’s easy no ? But where did i made a mistake as it does not work…

@Mikebecl : i read and read and read the doc but there is something obvious for you i can’t see…

I’m sure this kind of little automatic calculation are funny for you but please, help me to understand why this not working for me… Do you see an error in what i have done ?

Thanks for the help.
Xavier

you have a missing } at the end, the one that closes the class

you always have to check your logs for errors

        } // end of calculagefunction
}
?>

btw, your code is working

best regards

Hi Mike,
Thanks for your “eye”. You can imagine : when you are trying to understand what is wrong in the logic, you miss stupid thing like this.
So, thanks to you and “Item” for the help you gave me.

I test it and it’s working.
I have an additionnal question. As i put this calculation on an existing install and as there is some records for the contact, what do you think to update all the contacts ?

  • Is there a trick to run this logic hook for all the database once ?
  • do i have to write a query in phpmyadmin to export the birthdate, calculate outside and reimport the values calculated ?
  • Another solution ?

Again, thanks for your help :slight_smile:
Xavier

calculated age should always be on the fly, if you ask me, I would change your logic hook type to an after retrieve, so the age is always calculated with the contact is being retrieved from the database, so in this case you will always have the real age as of that date, even you can touch a bit the code and make an echo that is his/her birthday today if it is.

documentation http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_6.5/Module_Framework/Logic_Hooks/Module_Hooks/after_retrieve/

best regards

Thanks Mike,
I will try.
I can have different hook (before save, after save, etc) in the same files as i works with the same contact module ?

Thanks,
Xavier

yes you can, just make sure that the classes and functions in each file don’t get repeated, and you need to declare each type of logic in the logic_hooks.php file

best regards

So, i can have for example :
In the logic hook :


$hook_array['before_save'][] = Array(1, 'calcul age', 'custom/modules/Contacts/calculageclass.php','calculageclass', 'myfirstfunction');
$hook_array['after_save'][] = Array(1, 'calcul age', 'custom/modules/Contacts/calculageclass.php','calculageclass', 'mysecondfunction');
$hook_array['after_retrieve'][] = Array(1, 'calcul age', 'custom/modules/Contacts/calculageclass.php','calculageclass', 'mythirdfunction');

and in my file calculage.php


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

class calculageclass {
        // Local static variable to store your sum
        protected static $var1 = 0;
//and all my other vars used in the functions below

        function myfirstfunction($bean, $event, $arguments) {
                // will be triggered in this example with the before save event

	}
	function mysecondfunction($bean, $event, $arguments) {
                // will be triggered in this example with the after save event

	}
	function mythirdfunction($bean, $event, $arguments) {
                // will be triggered in this example with the after retrieve event

	}
}//close the class or Mike will kick you... :)

Correct for you ?

Otherwise, i didn’t understand one more thing : in the logic hook, when you declare a class and function to execute, there is a number. I put “1” but i could put “2” or “3” ? I see that’s an order, but how is this used for ?

Thanks Mike for your time :slight_smile:
Xavier

take a look at this example http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_6.5/Module_Framework/Logic_Hooks/Examples/Comparing_Bean_Properties_Between_Logic_Hooks/

the counter is for each type of logic hook, I recommend to you to use different files, different classes and different functions, at least for me is easy to debug

<?php

$hook_version               = 1;
$hook_array                 = Array();
$hook_array['after_save']   = Array();
$hook_array['after_save'][] = Array(
        1,
        'after_save example',
        'modules/Contacts/after_save.php',
        'logic_hooks_class_1',
        'after_save_method_1'
);
$hook_array['before_save']   = Array();
$hook_array['before_save'][] = Array(
        1,
        'before_save example',
        'modules/Contacts/before_save.php',
        'logic_hooks_class_2',
        'before_save_method_1'
);
$hook_array['after_retrieve']   = Array();
$hook_array['after_retrieve'][] = Array(
        1,
        'after_retrieve example',
        'modules/Contacts/after_retrieve.php',
        'logic_hooks_class_3',
        'after_retrieve_method_1'
);

?>

best regards

Ok, i see what you mean for the different files. That’s a really good idea to avoid messing all and find where i have issue easier.

I did not understand what is the counter even with your explanation. Don’t loose your time : i will try to find out.

Again and again and again : thanks :slight_smile:
Xavier

Just a little word to end this post : all is fine now on my side for this logic hooks use.

Again, thanks to all people who have helped me here :slight_smile:
This post can be closed (don’t know if this is needed on this forum :wink:
Xavier