How to create a logic hook based on info in subpanels

Dear Friends,

I have a field in leads module as ‘interested_c’ which may contain following values: blank, Yes, No

Here is the detailview of Campaign subpanel. One record has Activity Type = Click-thru Link and corresponding ‘related’ which is a URL (tracker).

I want to create a logic hook for leads module, which if possibly, could use both pieces of information as conditions:

if Activity Type = Click-thru Link and other ‘related’ = ,
then change interested_c = Yes

How such logic hook can be created?

Soliciting your help,

With thanks,

RK

You will need to add an “after_relationship_add” logic hook on the campaigns module.

See the documentation

In the hook logic, you can inspect the values of the relationship and bean to determine whether or not you want to set the value of interested_c to Yes.

Dear mminnie,

I actually needed working example of this sort or example of relating with INFO contained in ‘Campaign’ Subpanel.

With thanks,

RK

I don’t have the custom field on my system and I don’t have the Campaigns module enabled. With that being said, I will give you some pseudo-code as a start.

You will need to create a file custom/modules/Campaigns/logic_hooks.php

Contents of logic_hooks.php


    $hook_version = 1;
    if (!isset($hook_array) || !is_array($hook_array)) $hook_array = array();

    $hook_array['after_relationship_add'] = Array();
    $hook_array['after_relationship_add'][] = Array(1, 'Update interested_c', 'custom/modules/Campaigns/campaignsHookLogic.php','classCampaignsHookLogic','CampaignsAfterRelationshipAdd');
    );

Next you’ll need to create a file campaignsHookLogic.php referenced in the above code.


    if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
   
    class classCampaignsHookLogic
    {
        function CampaignsAfterRelationshipAdd ($bean, $event, $arguments)
        {
			 if ($arguments['related_module'] =='Leads'){
              	  	        $lead_bean  = BeanFactory::getBean('Leads', $arguments['related_id']);
                                if ($lead_bean->campaign_type = "Click-thru" && $lead_bean->refer_url = "xyz") {
                                   $lead_bean->interested_c = "Yes";
                                   $lead_bean->save();
                                }

			 }
		}
    }

Untested code, but this should work.

1 Like

Dear mminnie,

Thanks for giving me direction.

I was thinking of adding the logic to custom>modules>leads>logic_hook.php. You have taken contrasting approach. You have suggested me to build this logic in custom>modules>campaign>logic_hook.php.

Q. How do we decide that which module is that where we need to make these logic hooks?

Is the deciding factor is ‘Primary Module’ of the relationship, as shown in the following graphic?

Kindly confirm, so as to help me understand things correct.


When I go to Studio>Campaign>Fields, there is no ‘Activity Type’:

The question arises is then, - how to find such fields which are hard to find? For example, what if we want to use info from any other subpanel?

You know no every module or submodule is defined in studio.

You would understand that I had wasted a lot of time guessing, searching, imagining where is this field. The manual searching of such fields is a huge waste of time and energy.

Let us come back to this mysterious field ‘Activity Type’, may be it is defined in modules>CampaignLogs>vardefs.php.

I wish if there would be any easier way of finding such fields or info.

Kindly help me in further revision of Contents of logic_hooks.php (you have previously explained)

With thanks,

RK

Your question:

Q. How do we decide that which module is that where we need to make these logic hooks?

I don’t know the answer to this. For my changes, I usually key off of the primary module. I would have to ask or test whether or not the relationship hook works for the secondary module.

The CampaignLogs module contains a field activity_type to which you refer. I am unsure of whether you would want the logic hook on Campaign or CampaignLogs. To determine this, I would advise you to test a logic_hook on both modules and debug using a good debugger to trace through the code. You can start debugging in the hooks to see if you can grab the appropriate CampaignLog, Campaign, and Lead bean.

SuiteCRM takes time to learn how it works under the hood. These forums provide some good information, but not every answer is here. I have found it invaluable in learning SuiteCRM by using a good debugger to trace through the code. I use Netbeans with Xdebug on a LAMP setup.

Start some digging and report back with your progress.

I’m still not able to make progress!

Wish Someone help with more more accurate build up of logic hook.

RK

Did you get a debugger setup? What code changes have you made? Can you paste your current code with change?

Dear mminnie,

I had to replace text field ‘interested_c’ with ‘eng_sum_c’ which is decimal type and would store numeric values.

I now need to create a logic hook which if possibly, could use both pieces of information as conditions on each occurance of:

if Activity Type = " link" (Click-thru Link) (link) and other ‘related’ = (or tracker_id),
then the incremental value is added to custom field ‘eng_sum_c’.

Only things I could figure out is by now are:

This ‘Campaign’ Subpanel is getting most data from module: CampaignLog (which has a relationship with Campaign). [Corresponding MySQL Table: campaign_log]

These variables are defined in vardefs.php of suitecrm>modules>CampaignLog> Vardefs of the fields concerned here

There is no directly relationship of any kind between ‘Campaignlog’ and ‘Leads’. However, CampaingLog does have a relationship with Campaign module.

My current status is that I am not able to make sense of anything of all these scattered pieces of information.

My logic_hook.php is

logic_hook.php

<?php

$hook_version = 1; 
$hook_array = Array(); 
// position, file, function 
$hook_array['after_relationship_add'] = Array(); 
$hook_array['after_relationship_add'][] = Array(1, 'Engagement Scoring via CampaignLogs', 'custom/modules/CampaignLogs/ENGScoring.php', 'SUM', 'ENGScore');
?>

My ENGScoring.php is not making sense to me even.

<?php

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

class SUM
{
    function ENGScore($bean, $event, $arguments)
    {
    	if ($arguments['related_module'] =='Campaign')
    	{
	        $leadsBean  = BeanFactory::getBean('CampaignLog', $arguments['target_id']);

            if ($bean->activity_type == "link")
            // && $campaignBean->refer_url == "https://github.com/salesagility/SuiteCRM/pull/2454/commits/1a09fd2cffde60533c0fd03d1f33cfcc8e1e7105") 
            {
               $lead_Bean->eng_sum_c = '55';
               $lead_Bean->save();
            }
    }
}         

Please help in building this logic hook definition and underlying logic.

With thanks,

RK

Try this:

logic_hooks.php - Notice the type of hook changed.


<?php

$hook_version = 1; 
$hook_array = Array(); 
// position, file, function 
$hook_array['after_save'] = Array(); 
$hook_array['after_save'][] = Array(1, 'Engagement Scoring via CampaignLogs', 'custom/modules/CampaignLogs/ENGScoring.php', 'SUM', 'ENGScore');
?>

ENGScoring.php


<?php

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

class SUM
{
    function ENGScore($bean, $event, $arguments)
    {
        if ($bean->target_type == "Leads" && $bean->activity_type == "link" && $bean->related_type == "CampaignTrackers") {
           $campaign_tracker = BeanFactory::getBean("CampaignTrackers", $bean->related_id);
           if ($campaign_tracker && $campaign_tracker->refer_url == https://github.com/salesagility/SuiteCRM/pull/2454/commits/1a09fd2cffde60533c0fd03d1f33cfcc8e1e7105") 
            {
               $lead_Bean = BeanFactory::getBean("Leads", $bean->target_id);
               $lead_Bean->eng_sum_c = '55';
               $lead_Bean->save();
            }
    }
}         

Dear mminnie,

The code mentioned in ENGScoring.php is not working as of now, I have tried it . I also have tried it with making some alterations in coding practices too.

For example,
$campaign_tracker = BeanFactory::getBean(“CampaignTrackers”, $bean->related_id);

I tried the above with ‘CampagingTrackers’ instead of 'CampaignTrackers":
$campaign_tracker = BeanFactory::getBean(‘CampaignTrackers’, $bean->related_id);

I also tried with:
$leadBean = BeanFactory::getBean(‘Leads’, $bean->target_id);
$leadBean->eng_sum_c = ‘55’;
$leadBean->save();

$lead_Bean was replaced with $leadBean.

May be we are not getting correct hold of writting conventions for beans! There is something concerning this in Jim Mackin’s book ‘SuiteCRM for Developers’ .

@mminnie, Building this logic hook is becoming insurmountable to me.

With thanks,

RK

@mminnie

I wonder if you might be able to help with a related but not quite the same issue for me?

https://www.suitecrm.com/forum/suitecrm-7-0-discussion/12542-making-a-field-update-after-a-response-from-email

My mistake in the pseudocode. I didn’t use the correct module name for either. To verify the name of the module to use, look in the modules/CampaignTrackers/CampaignTracker.php file. The name of the bean is CampaignTracker. No “s” at the end. Same goes for leads…it is the Lead bean.

Make those changes and try again.

And…I am getting the impression you are not stepping through with a debugger???

@mminnie,

Your hunch or intuition is absolutely correct.

I thought about using NetBeans, I downloaded it. But was taken aback with the advanceness of its functionality. It would be quite a steep learning curve, to get going with it with SuiteCRM. I haven’t used NetBeans before.

Well, I will try that.

With thanks,

RK

Being able to step through the code and evaluate values of variables is a must if you going to do any SuiteCRM development.

Dear mminnie,

I am struggling with NetBeans as well as logic hook.

What I could look after having initial sessions with Netbeans:

<?php

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

class SUM
{
    function ENGScore($bean, $event, $arguments)
    {
        if ($bean->target_type == "Leads" && $bean->activity_type == "link" && $bean->related_type == "CampaignTrackers") 
        {
           $CampaignTracker = BeanFactory::getBean('CampaignTracker', $bean->related_id);
           $TrackerUrl = BeanFactory::getBean('Campaign', $bean->tracker_url);
           if ($CampaignTracker && $TrackerUrl === "https://github.com/salesagility/SuiteCRM/pull/2454/commits/1a09fd2cffde60533c0fd03d1f33cfcc8e1e7105") 
            {
               //$bean->log_sum_c = '505';
               $lead = BeanFactory::getBean('Lead', $bean->target_id);
               $lead->eng_sum_c = '55';
               $lead->save();
            }
    }
}   

Could you please see the last 3-lines where we are attempting to pass values to Leads, if those are correctly written or not?

What it would take to see if this logic hook is firing or not?

For example, visiting the hyperlink with tracker:
http://localhost/luminisindia/SuiteCRM/index.php?entryPoint=campaign_trackerv2&track=c25c9f86-007f-8af5-8521-587291a07d39&identifier=8f237571-4eba-ac21-4d2b-58737d8c25d4

or sending another email to lead, so that to make a new record in campaign log?

Also have a look at the following JPEGs taken from NetBeans:

The lines in your hook function look correct.

To see if the code is even executing, you could simply put a logging line in the function like:


file_put_contents("mylogfile.log", "Code executed!\r\n", FILE_APPEND);

If the file mylogfile.log appears…it ran!

As for the Netbeans/Xdebug…it looks like Xdebug isn’t setup correctly. It shouldn’t be waiting for Xdebug for long. And…since the URL parameter shows “DEBUG_SESSSION_START=netbeans-xdebug”…tells me you probably setup the properties of your Netbeans project incorrectly. You won’t be accessing your logic hook function directly like you have shown. Your project properties should point to index.php like:

Once you setup Xdebug correctly, Netbeans won’t be waiting for it and the browser will be redirected to index.php after Xdebug takes control. Basically you shouldn’t see the “DEBUG_SESSSION_START=netbeans-xdebug” in the URL.

You can figure out how to get Xdebug running with Netbeans by some Google searches. There are some php.ini settings to enable. Google will be your friend here.

Once setup, you should be able to put a breakpoint on your hook logic function.

As far as hitting a URL to fire the code…I’m not sure if the entry point URL will insert a record upon each fire…or only on the first hit. Depends on how it is programmed. The safest bet would be a new email…thus creating a new link with new parameters.

Fortunate for you I had to reinstall php and xdebug…so I can give you some guidance on Xdebug.

You will want to issue the commands:


sudo apt install php-xdebug
sudo service apache2 restart

Then edit the xdebug.ini. This is assuming you have php7.0 and your xdebug.ini is in the same directory.


sudo vi /etc/php/7.0/apache2/conf.d/20-xdebug.ini

In the INI you will want:


zend_extension=xdebug.so
xdebug.show_error_trace = 1
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_port=9000
xdebug.remote_autostart=0
xdebug.remote_connect_back=0

You should now be able to set a breakpoint and inspect variables.

Dear mminnie,

Thanks for your helpful instructions.

I could get Netbeans & xDebug get going.

I also could get xDebub.log running.

When I start ‘Debug the project’ it works very well and starts with index.php and logs the info into xDebug.log too.

But now I am facing the problem that when I look to debug the file the file ENGScoring.php, xDebug keeps waiting and waiting…

Now I don’t know how to proceed with debugging of this file.

Please guide.

With thanks,

RK

Great news on getting it running.

Once you have it running, you can just set a breakpoint in any PHP file. You don’t specifically run the debugger for a single PHP file…you always start it at index.php. Just keep browsing in any same-browser tab and when that PHP file executes, the debugger will stop on that breakpoint line. At that point the browser is still “loading” waiting for the server response.

So…get it to the “running…” point and then hit the URL that will trigger your ENGScoring.php code.

Note: In your screen capture, your ENGScoring.php file has a red ! exclamation point…meaning you have PHP code with an error.