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

  • it is possible that the $rel_name is not correct, it is also possible that the name is different depending on which side of the relationship you are looking at.

    The name that you see in studio is not necessarily the name of the relationship itself.

    For example the account_leads relationship is called "accounts" when viewed from the Lead side, but it's called "leads" from the Account side. So if the bean is Leads you need to use "accounts" if the bean is Accounts then you need to use "leads" as the link name.

    Check 

    cache/modules/Leads/Leadvardefs.php

    find the relationship in the vardefs and check the "name" field, that is what you should use a link name in the load_relationship

    You can also add an additional check in your code:

    Fullscreen
    1
    2
    3
    4
    5
    if($bean->load_relationship($rel_name)){
    //then proceed;
    }else{
    //log the error so you know the relationship is not right
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    FrancescaS

  • You cannot link a Lead before_save to a project because this Leads does not yet exist at that time.

    You have to use after_save instead.

  • I've tried after_save but no luck.

  • Thanks.  We're on Sugar Cloud so the cache files are not readily accessible.  I've requested Sugar support for it.

    In the meantime, I've tried your last suggestion and it does appear that 'project_leads_1' is the only one that doesn't throw an error but it still doesn't link the Lead record to the project.  I'm using the after_save logic hook, as Harald suggested.

    $rel_names = array('projects', 'project', 'project_leads_1', 'project_leads', 'projects_leads');
    foreach($rel_names as $rel_name)
    {
      if($bean->load_relationship($rel_name))
      {
        $GLOBALS['log']->fatal('----- relationship name: ' . $rel_name . ' -----');
        if($bean->last_name == 'Incoming Email Sales Lead')
        {
          ...
        }

      }
      else
      {
        $GLOBALS['log']->fatal('----- Error: Incorrect relationship name: ' . $rel_name . ' -----');
      }

    }

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

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <?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'
    );
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <?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);
    }
    }
    }
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    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.

  • This is only a snippet to show that the logic_hook works fine.

    I used the hard coded $p1 just as an example. Much better would be to have a local flag or string in projects which declares a dedicated project as the collector project. Searching this local field could return the id of the dedicated project into $p1.

    Thanks  for reviewing!

  • You can download the custom relationship definitions by an Customization Export in admin - studio. Click on Export Customizations. Select Leads and Projects. The exported zip file contains the varfeds of the custom relationships in Extensions/modules/*/Vardefs directories.

  • Sorry , I misinterpreted the post (thought it was the original poster saying it still didn't work) and later removed my comments, trying to do too many things at once again. Will be more careful.

  • I appreciate your reviews.

    Thanks for any discussion.

1 2