Link a Lead record to a Project record using logic hook

I have created a one-to-many relationship between Project and Lead records. (called 'project_leads_1' in Studio)

I would like it so that when a new Lead record is created that meets certain criteria (last name = '@Incoming Email Sales Lead'), the Lead record is linked to a specific Project Record ('Incoming Email Sales' Project).

However, it's not working.  What am I missing?

I'm using 'before_save' logic hook on the Leads module

.....

$rel_name = 'project_leads_1';  // name of the relationship in Studio

$incoming_sales_leads_proj = [hard coded ID];

$incoming_service_leads_proj = [hard coded ID];
if($bean->last_name == '@Incoming Email Sales Lead')
{
$bean->load_relationship($rel_name);
$bean->$rel_name->add($incoming_sales_leads_proj);

}
else if($bean->last_name == '@Incoming Email Service Lead')
{
 $bean->load_relationship($rel_name);
 $bean->$rel_name->add($incoming_service_leads_proj);
}

......

Sugar 13.0

Parents
  • I created a logic_hook in custom\Extension\modules\Leads\Ext\LogicHooks\setLinkLead.php:

    <?php
    
    $hook_version = 1;
    if (!isset($hook_array)) {
        $hook_array = array();
    }
    
    if (!isset($hook_array['after_save'])) {
        $hook_array['after_save'] = array();
    }
    
    $hook_array['after_save'][] = array(
        200,
        'after Lead is saved',
        'custom/modules/Leads/linkLeadHook.php',
        'linkLeadHook',
        'linkLead'
    );
    

    and the implementation of the hook in custom\modules\Leads\linkLeadHook.php:

    <?php
    class linkLeadHook
    {
        public function linkLead($bean, $event, $arguments)
        {
    ////	$GLOBALS['log']->fatal("LEAD HOOK A = ".print_r($arguments,true));
    	   
    	    if (($arguments['isUpdate'] != 1)&&($bean->deleted == 0)) {
    			
    			if ($bean->last_name == "TEST") {
    				
    				$rel = "project_leads_1";
    				$p1 = "1770f528-26d5-11ee-98a0-54ee7593936c";
    				$GLOBALS['log']->fatal("LEAD HOOK SHOULD DO STH.");
    				if($bean->load_relationship($rel))
    				{
    					$bean->$rel->add($p1);
    				}
    			}
    		}
        }
    }
    

    If the Lead is new and if its last_name is "TEST" a relationship to Project P1 with id "1770f528-26d5-11ee-98a0-54ee7593936c" is created.

Reply
  • I created a logic_hook in custom\Extension\modules\Leads\Ext\LogicHooks\setLinkLead.php:

    <?php
    
    $hook_version = 1;
    if (!isset($hook_array)) {
        $hook_array = array();
    }
    
    if (!isset($hook_array['after_save'])) {
        $hook_array['after_save'] = array();
    }
    
    $hook_array['after_save'][] = array(
        200,
        'after Lead is saved',
        'custom/modules/Leads/linkLeadHook.php',
        'linkLeadHook',
        'linkLead'
    );
    

    and the implementation of the hook in custom\modules\Leads\linkLeadHook.php:

    <?php
    class linkLeadHook
    {
        public function linkLead($bean, $event, $arguments)
        {
    ////	$GLOBALS['log']->fatal("LEAD HOOK A = ".print_r($arguments,true));
    	   
    	    if (($arguments['isUpdate'] != 1)&&($bean->deleted == 0)) {
    			
    			if ($bean->last_name == "TEST") {
    				
    				$rel = "project_leads_1";
    				$p1 = "1770f528-26d5-11ee-98a0-54ee7593936c";
    				$GLOBALS['log']->fatal("LEAD HOOK SHOULD DO STH.");
    				if($bean->load_relationship($rel))
    				{
    					$bean->$rel->add($p1);
    				}
    			}
    		}
        }
    }
    

    If the Lead is new and if its last_name is "TEST" a relationship to Project P1 with id "1770f528-26d5-11ee-98a0-54ee7593936c" is created.

Children