Help with Logic Hook

Hi All,
I’ve been trying to understand how to develop logic hooks for my SuiteCRM instance and was looking for some help. I’ve read several of the guides which have not made it any easier to understand. What I’m trying to achieve is the following.

I’ve created a custom module named Candidates. In this module I have fields named candidate_name and candidate_no and I’ve created a relationship one to many with the tasks module. In the task module I’ve created two custom fields named candidate_name_c and candidate_no_c and customised the my open tasks dashlet to display these two new fields.

What I want to do is when creating a task using the sub panel in the Candidates Module is to have the candidate_name_c and candidate_no_c within tasks to be populated with the data from candidate_name and candidate_no from the Candidates Module. This is so on the dashlet users can see the candidate name and number the task relates to.

Any help would be greatly appreciated.

An update on this request for help. Using a post by Arsalan found here https://suitecrm.com/forum/suitecrm-7-0-discussion/12823-workflow-action-to-populate-field#43249 what I’ve done is created a before_save logic hook in custom/modules/Tasks/logic_hooks.php. The file is as follows

<?php hook_array['before_save'][] = Array(1, 'before_save_candidates', 'custom/modules/candidates/before_save_class.php', before_save_class', before_save_method'); ?>

I’ve then created a logic hook before_save_class.php in custom/modules/candidates/. The file is as follows

<?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); class before_save_class { function before_save_method($bean, $event, $arguments) { //canidate field in Tasks is candidate_tasks //We will use beans to fetch the related candidate details $candidate = new Task(); $candidate->retrieve($bean->candidate_tasks); //Set the values from Candidates into Tasks // $bean is your current record being saved. // $candidate is the related candidate. $bean->candidate_name_c = $candidate->name; $bean->candidate_no_c = $candidate->candidate_no; //Same stuff in order to set other values } } ?>

After running a repair and trying to create a task and clicking save nothing happens. No task is created and no error shown. Checking the event logs I see the following entries

[Fri Sep 08 13:54:54.988730 2017] [:error] [pid 2628:tid 1968] [client 172.16.102.26:58713] PHP Fatal error: Cannot use [] for reading in C:\xampp\htdocs\suitecrm\custom\modules\Tasks\logic_hooks.php on line 2, referer: http://idweb01/suitecrm/index.php?action=ajaxui
[Fri Sep 08 13:57:32.180624 2017] [:error] [pid 2628:tid 1940] [client 172.16.102.26:58771] PHP Fatal error: Cannot use [] for reading in C:\xampp\htdocs\suitecrm\custom\modules\Tasks\logic_hooks.php on line 2, referer: http://idweb01/suitecrm/index.php?action=ajaxui
[Fri Sep 08 14:06:47.754984 2017] [:error] [pid 2628:tid 1960] [client 172.16.102.26:58926] PHP Fatal error: Cannot use [] for reading in C:\xampp\htdocs\suitecrm\custom\modules\Tasks\logic_hooks.php on line 2, referer: http://idweb01/suitecrm/index.php?action=ajaxui

Not sure what I’ve done wrong but any help would be appreciated

Please edit your post (or post again) this time putting all your code and logs inside the forums’ code tags. That way we can see which brackets you used and make sure the syntax is correct… the way it is now, the forums remove parts of what you posted…

Hi,
I’ve resubmitted my code as follows. My logic_hook.php file is as follows


<?php

hook_array['before_save'][] = Array(1, 'before_save_candidates', 'custom/modules/candidates/before_save_class.php', before_save_class', before_save_method');

?>

My before_save_class.php is as follows.


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

class before_save_class
{
function before_save_method($bean, $event, $arguments)
{
//canidate field in Tasks is candidate_tasks
//We will use beans to fetch the related candidate details
$candidate = new Task();
$candidate->retrieve($bean->candidate_tasks);
//Set the values from Candidates into Tasks
// $bean is your current record being saved.
// $candidate is the related candidate.
$bean->candidate_name_c = $candidate->name;
$bean->candidate_no_c = $candidate->candidate_no;
//Same stuff in order to set other values

}
}
?>

Amended logic_hook.php as missing $ when calling the array.
Code amended to the following


<?php

$hook_array['before_save'][] = Array(1, 'before_save_candidates', 'custom/modules/candidates/before_save_class.php', before_save_class', before_save_method');

?>

You’re missing an apostrophe when adding to the hook array:

, before_save_class'

should be

, 'before_save_class'

Not sure where I’m going wrong here or whether I’m referencing the wrong tables.
I’ve attached images of my setup along with my code.

\custom\modules\CAN_Candidate\before_save_class.php contains the following code


<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
   
    class before_save_class
    {
        function before_save_method($bean, $event, $arguments)
        {
            //lets assume the candidate field in tasks is candidate_tasks
           //We will use beans to fetch the related candidate's details
           $candidate = new Task();
           $candidate->retrieve($bean->can_candidate_tasks_c);
           //Set the values from candidates into tasks
           // $bean is your current record being saved. 
           // $candidate is the related candidate.
           $bean->candidate_name_c = $candidate->name;
           $bean->candidate_no_c = $candidate->candidate_no;
           //Same stuff in order to set other values

        }
    }
?>

\custom\modules\Tasks\logic_hooks.php file contains the following code


<?php
$hook_version = 1;
$hook_array['before_save'][] = Array(
        //Processing index. For sorting the array.
        1,
       
        //Label. A string value to identify the hook.
        'before_save_candidate',
       
        //The PHP file where your class is located.
        'custom/modules/CAN_Candidate/before_save_class.php',
       
        //The class the method is in.
        'before_save_class',
       
        //The method to call.
        'before_save_method'
    );
?>

You can see on the attached Capture1.png the tasks subpanel should has two custom fields that should be filled with Candidate Name and Candidate No from the Canidate module but this is not happening.

Let me know if there’s anything else I can provide to help with fixing the issue.

I think you might need to initiate the array before adding elements to it.

This is an example from Jim Mackin’s eBook:


$hook_version = 1;
$hook_array = Array();
$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(
                               77,
                               'updateGeocodeInfo',
                               'custom/modules/Cases/CasesJjwg_MapsLogicHook.php',
                               'CasesJjwg_MapsLogicHook',
                               'updateGeocodeInfo');

Notice the 2nd and 3rd lines.