after_relationship_add hook in subpanel results in empty data

I wrote a logic hook for the accounts module that connect to external integration script. And that works just fine.
However I need to trigger the logic hook when you add, edit or delete the relationship sub panel.

this is the logic hook code in the Accounts

$hook_array['after_relationship_add'][] = Array(
    1,
    'addRelationship',
    'custom/modules/Accounts/Ext/LogicHooks/exportAccount.php', // file
    'exportAccount_LogicHook', // class
    'export' // function
);

And it looks like it triggers the logic hook just fine however it results in the empty data from added in sub panel.

It looks to me like the hook is triggered before the data is saved in the database.

When I saved the array in the logs this is what I get:

(
    [] => Array
        (
            [dlr_program] => 
            [lss_dealer_nbr] => 
            [portfolio_id] => 
        )

)

Can someone help me figure out as to why this happens?
I want to hook in the sub panel be triggered after the data are saved so I can retrieve them.

Someitmes more than one logic hook is called in one operation. You can check if some other hook is getting called, that you can use (say, an after_save hook on the records on each side of the relationship).

I fyou post all the code it may be easier to try to understand it.

Without the logic it is just impossible to help!

There is a logic hook file in modules/Accounts/logic_hook.php. In that file I have the accounts after save logic added to call the exportAccount.php logic hook along with additional after_relationship_add and after_relationship_delete:

$hook_array['after_save'] = Array();
$hook_array['after_save'][] = Array(
    1,
    'export Account after save', // label
    'custom/modules/Accounts/Ext/LogicHooks/exportAccount.php', // file
    'exportAccount_LogicHook', // class
    'export' // function
);

$hook_array['after_relationship_add'] = Array();
$hook_array['after_relationship_add'][] = Array(
    1,
    'addRelationship',
    'custom/modules/Accounts/Ext/LogicHooks/exportAccount.php', // file
    'exportAccount_LogicHook', // class
    'export' // function
);

$hook_array['after_relationship_delete'] = Array();
$hook_array['after_relationship_delete'][] = Array(
    1,
    'deleteRelationship',
    'custom/modules/Accounts/Ext/LogicHooks/exportAccount.php', // file
    'exportAccount_LogicHook', // class
    'export' // function
);

What I wanted was in an event of adding information in sub-panel I wanted to trigger the same action. Just sending a request to external program to process information.
The trigger works for sub-panel however the sub-panel information is not there. It looks to me like sub-panel info is saved into database after the export is triggered.

What the export does is connecting to the suitecrm database and pull necessary information and exports it to the external site.

Export hook code:

require_once 'include/SugarHttpClient.php';

class exportAccount_LogicHook
{

    protected $client;

    /**
     * REST client
     *
     * @var string
     */
    function __construct()
    {
        $this->setClient(new SugarHttpClient());
    }

    /**
     * Set client to talk to SNIP
     *
     * @param SugarHttpClient $client
     */
    public function setClient(SugarHttpClient $client)
    {
        $this->client = $client;
        return $this;
    }

    public function export($bean, $event, $arguments)
    {
        $vars = [];
        file_put_contents("mylogfile.log", "export func\r\n", FILE_APPEND);

        if (isset($bean->fetched_row['id'])) {
            file_put_contents("mylogfile.log", "- id " . $bean->fetched_row['id'] . "\r\n", FILE_APPEND);

            $vars['action'] = 'modifyDealer';
            $vars['key'] = 'no';
            $vars['val'] = $bean->fetched_row['dealer_nbr_c'];
        } else {
            file_put_contents("mylogfile.log", " new dealer \r\n", FILE_APPEND);

            $vars['action'] = 'modify';
            $vars['flag']['new'] = true;
        }
        // sleep(10);
        $this->exportJob($vars);
    }

    /**
     * Execute given job
     *
     * @param SchedulersJob $job
     */
    public function exportJob($info)
    {
        file_put_contents("mylogfile.log", "execute func\r\n", FILE_APPEND);

        global $sugar_config;

        $data = http_build_query($info);

        $response = $this->client->callRest($sugar_config['export_url'], $data);
        // file_put_contents("mylogfile.log", $response . "\r\n" . $data . "\r\n", FILE_APPEND);
        file_put_contents("mylogfile.log", $response . "\r\n" . $data . "\r\n", FILE_APPEND);
    }
}

Should I trigger the after_relationship_add part from the sub-panel after_save rather than from accounts logic hook?

I believe what is happening in this particular case is during the process of adding a relationship into tables dlr_dealer_programs_accounts_c (a relationship table) and dlr_dealer_program (program storage table) is the record is inserted into relationship table and the request is being fired and program storage table data are inserted after the response is received from the request.

I was able to deduce this from the logs. I change the sequel to the right join and the program data returned an empty.

This defeats the purpose of the whole request. How can I fix that?

I don’t really know the answer for you, but what I’ve seen is that there are normally ways to know more about what is calling the hook, but looking at the arguments.

So from a quick scan online I find a thread with things like

    if (isset($bean->id) && ($arguments['related_module'] == 'some_module')) {
$d->retrieve($arguments['related_id']);
if (!empty($arguments['related_module'] ) && $arguments['related_module']== 'c_Custom_Module' && !empty( $arguments['related_id']){

And this one with fetched_row which you can use to determine if it is a new record or an edit:

     if (isset($bean->fetched_row)) {

So you can try to see what these values have when your hooks are called, and that might give you a way to distinguish different hook calls.

I hope you’re using XDEBUG and a debugger… :slight_smile:

ok maybe I am understanding it wrong but when we add a relationship crm produces 2 tables. one data table and one relationship table. what is happening here with after save is that the relationship data is added but the data table is empty.
this system completely doesn’t make any sense. how can there be relationship added but not data ? both should be added at the same time and then the logic hook executed.

how can there be after relationship logic to execute when the data of that relationship doesn’t exists.

Maybe it doesn’t exist yet, but will exist in a second (unless a PHP fatal error breaks the flow of the code).

I am having same issue. After_relationship_add Loggic hook not working after save for subpanel. if you got the solution, Could you please share.

Thanks